#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    4
    Rep Power
    0

    Question getting javascript error when using PHP and javascript


    I'm having some problems with some code I've been working on. I'm creating a form in HTML with 3 combo boxes. The data for the combo boxes is filled dynamically from arrays in javascript. The arrays are populated using PHP to access a mySQL database.
    The selection made in the first combo box will decide what is available in the second combo box and the choice in the second combo box affects the third. I think I got the hard part taken care of and a look at "View Source" is giving me a good indication that my PHP is creating the arrays correctly for the javascript. The problem comes as soon as I make my selection in the first combo box I get a javascript error saying that the array that the javascript function calls is undefined. Here is the line that the error points to:

    <SELECT id=sbuChoice name=sbuChoice onchange="selectChange(this, productChoices.familyChoice, arrFamilyName, arrFamily);">

    It specifically mentions arrFamilyName which is one of the arrays.
    Here's the code for the definition of the arrary and the populating of it:

    var arrFamily = new Array();
    var arrFamilyName = new Array();

    <?php
    mysql_select_db("solutions")
    or die("Could not select database");
    $sql = mysql_query("SELECT * FROM sbu");
    while($row=mysql_fetch_row($sql)) {
    $sbu_id = $row[0];
    $sbu_name = $row[1];

    $sql2 = mysql_query("SELECT * FROM family where sbu_id=$sbu_id");
    while ($row2=mysql_fetch_row($sql2)) {
    $family_name = $row2[0];
    $family_id = $row2[1];
    echo "arrFamily[] = ".$sbu_id.";";
    echo "arrFamilyName[] = \"".$family_name."\";";
    }
    }
    ?>


    The array clearly appears to be defined and I can't understand why I am having a problem. I think it may be related to the PHP only because the same javascript code will work if I take out all the PHP and put in static arrays.

    If anyone has any ideas that would be greatly appreciated.
    Let me know if you need to see part of the code I left out.

    Thanks again.

    -Steve
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    What does the same code look like after the page has been built? The javascript code block that is.
  4. #3
  5. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Looking at the code structure for an array in javascript, you may be using it wrong. Not for sure, but how about trying something like this:
    PHP Code:
    <?php
    mysql_select_db
    ("solutions") or die("Could not select database");
    $sql mysql_query("Select * from sbu");
    while(
    $row mysql_fetch_row($sql)) {
        
    $sbu_id[] = $row[0]; //we will use this to build the js array
        
    $sbu_name $row[1]; //really don't understand why you are gathering this, it is not being used

        
    $sql2 mysql_query("Select * from family where sbu_id=$row[0]");
        while(
    $row2 =  mysql_fetch_row($sql2)) {
            
    $family_name[] = $row2[0];
            
    $family_id $row2[1];  //really don't understand why you are gathering this, it is not being used
        
    }
    }
    $arrFamily implode(",",$sbu_id);
    $arrFamilyName implode("\",\"",$family_name); //this one should combine to "element1","element2","etc..."
    echo "var arrFamily = new Array($arrFamily);\n";
    echo 
    "var arrFamilyName = new Array($arrFamilyName);\n";
    ?>
    Last edited by Onslaught; December 14th, 2001 at 08:31 PM.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    4
    Rep Power
    0
    This is copied directly from a "View Source":

    function selectChange(control, controlToPopulate, ItemArray, GroupArray)
    {
    //Generate all arrays for families
    var arrFamily = new Array();
    var arrFamilyName = new Array();

    arrFamily[] = 1;arrFamilyName[] = "Controller";arrFamily[] = 1;arrFamilyName[] = "Settop";arrFamily[] = 1;arrFamilyName[] = "Headend";arrFamily[] = 2;arrFamilyName[] = "Controller";arrFamily[] = 3;arrFamilyName[] = "Cable Modem";


    The only thing I see off is the formatting which as far as I know doesn't matter.
  8. #5
  9. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Like I said in my last post, I think you are using the array wrong. I may be wrong, but I could find no reference where js using automatic incrementing indexes for arrays. This is the way you are using it.
    As far as I know the formatting does not matter, just easier to read.
    How do you declare the arrays(and values) when you do it without php?
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    4
    Rep Power
    0
    I think you're right....when I was doing it manually in javascript I was indexing them. I probably could index them the way I was doing but your way seems smarter and chews up less code. One problem I'm getting now though is I'm getting a Warning: Bad arguments to implode() on line 55

    I've entered it exactly as you had above and double checked the syntax for implode since I've never used it before. Is it possible my PHP server just sucks?
  12. #7
  13. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    try using single quotes instead:
    PHP Code:
    $arrFamilyName implode("','",$family_name); 
    //or like this
    foreach($family_name as $fn) {$arrFamilyName .= "\"$fn\",";}
    $arrFamilyName substr($arrFamilyName,0,(strlen($arrFamilyName)-1));  //to remove the last , this may need to be 2 instead of 1 
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    4
    Rep Power
    0

    Talking


    Looks like it's all working now! Thanks for all the help Onslaught!
  16. #9
  17. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793

IMN logo majestic logo threadwatch logo seochat tools logo