#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    35
    Rep Power
    12

    Question Writing Javascript with PHP


    I'm having a problem writing javascript with PHP and I was wondering if someone could point me in the right direction.

    Basically........when i have php "echo" the values of an array in a loop to the javascript portion of the page, it will output the array correctly but the script will not work. But when i just hard code the array into the javascript portion of the page the script works fine.

    Do I need to do some kind of output buffering? Is it possible that parts of the page are being downloaded by the browser before others.

    This is very frustrating because when I "view source" on the page, it looks identicle either way (PHP writing, or Hard Coded). So I'm pretty sure there is no error in my PHP.

    I need to have PHP write the values out because they are coming from mysql.

    Any help would be greatly appreciated.

    Travis
  2. #2
  3. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    16
    Post the PHP script.
    Alex
    (http://www.alex-greg.com)
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    35
    Rep Power
    12
    Here is the just the PHP portion, it is inserted between "<head>" tags.
    PHP Code:
    <?
    // Connect to DB
    $db_host "na"
    $db_user "na";
    $db_name "na"
    $db_pass "na";

    $conn mysql_connect("$db_host","$db_user","$db_pass"); 
    if (!
    $conn) die ("Could not connect MySQL"); 
    mysql_select_db($db_name,$conn) or die ("Could not open database");

    $user_name $_SESSION['user_name'];
    $query "SELECT * FROM rm_brackets WHERE user_name = '$user_name'"
    $result mysql_query($query);
    $numrows mysql_num_rows($result);

    if(
    $numrows 0){
         
    $row mysql_fetch_array($result);
        
        echo 
    "<script>\n\n";
        
        echo 
    "var PICKS = new Array(63);\n";
        for(
    $i=0$i<63$i++){
                echo 
    "PICKS[" $i "] = \"" $row[$i+1] . "\";\n";
        }
        
        echo 
    "\nfunction loadPicks(){\n";
        echo 
    "var idx2;\n";
        echo 
    "for(var i=1; i<64; i++){\n";
        echo 
    "idx2 = i - 1;\n";
        echo 
    "var listObj = document.forms[0].elements[\"game_\" + i];\n";
        echo 
    "listObj.options[2].text = PICKS[i-1];\n";
        echo 
    "listObj.options[2].selected = true;\n";
        echo 
    "}\n}\n";
        echo 
    "\n</script>\n";
    }
    ?>
    It is just supposed to print an array of values from mysql and a function for loading those values into "select" boxes. Those values are then loaded when a button is clicked.

    The weird thing is that when I put the for loop that writes the array in, it breaks all the script on the page even though the function hasn't been called yet.

    I can post the whole page if it helps.....but it is rather large and I think it might just scare some people off.

    Travis
  6. #4
  7. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    16
    Try echoing out \r\n instead of \n for a new-line.

    Also, try putting the JS function outside of PHP, e.g.
    PHP Code:

        for($i=0; $i<63; $i++){

                echo "PICKS[" . $i . "] = \"" . $row[$i+1] . "\";\n";

        }

        ?>
    function loadPicks(){
    var idx2;
    for(var i=1; i<64; i++){
    idx2 = i - 1;
    var listObj = document.forms[0].elements["game_" + i];
    listObj.options[2].text = PICKS[i-1];
    listObj.options[2].selected = true;
    }
    }
    </script>
    <?
    }

    ?>
    Alex
    (http://www.alex-greg.com)
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    35
    Rep Power
    12
    Tried both adding "\r" and taking the function out of php tags. Neither seems to do anything different.

    The thing that is really frustrating me is that what PHP is echoing is correct. When I choose "view source" on the page, the javascript looks fine. In fact, when I just hard coded the array into the page, i had copy and pasted it straight from "view source" of the bad page.

    Its like something is getting out of sinc and it doesn't like that array being printed by php.

    Travis
  10. #6
  11. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    16
    OK, post the full script (or a link to it) and some sample data from your database table (you can use the mysqldump program at the command line to dump out your database contents) and I'll test it out tomorrow. This sound quite wierd.
    Alex
    (http://www.alex-greg.com)
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    35
    Rep Power
    12
    Here you go.

    I hope you can see something, I am drawing a blank.

    Travis
    Attached Files

IMN logo majestic logo threadwatch logo seochat tools logo