0

ok so I have this in my HTML code:

<script type="text/javascript" src="load2.php"> </script>

I saw somewhere you could call a php file like that and the javascript contained in it will be rendered on the page once echoed. So in my PHP file i have this:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $storeArray[] =  $row['DayNum']; }
            $length = count($storeArray);

I connected to my database and stuff and pulled those records and stored them in an array. Now my problem is alerting them using js. This is what I have:

echo " function test() {

                for(var i = 0; i<$length; i++){
                    alert($storeArray[i]);
                }   
        }
            ";

The test() function is being onloaded in my HTML page, but for nothing the values in the array won't alert. Any help please?

4

3 回答 3

2
echo " function test() {

                for(var i = 0; i<$length; i++){
                    alert($storeArray[i]);
                }   
        }
            ";

This code is literally writing what you have written above. It's not completely clear, but I believe your intent is to loop over the contents of your database data, and alert that to the browser with alert() function.

You can achieve this in a couple of ways.

Write multiple alert statements

echo "function test() {"; //Outputting Javascript code.

for($i = 0; $i<$length; $i++){ //Back in PHP mode - notice how we aren't inside of a string.
    $value = $storeArray[$i];
    echo "alert($value)"; //Outputting Javascript code again.
}
echo "}"; //Outputting Javascript code to close your javascript "test()" function.

Write a Javascript array, then loop over it in Javascript

echo "function test() {";
echo "    var storeArray = ['" . implode("','", $storeArray) . "'];";
echo "    for (var i = 0; i < storeArray.length; i++) {";
echo "        alert(storeArray[i]);";
echo "    };";
echo "}";

Finally, you could use AJAX and JSON to load the data, rather than outputting a JS file from PHP. That is an entirely different topic, though, and you should search StackOverflow for more examples as there are numerous questions and answers involving it.

于 2013-07-25T15:20:38.507 回答
0

Unless your array contains only number, you probably have JS error. You should put your $storeArray[i] in quotes in the alert function so it considered as a string in js.

alert('$storeArray[i]');

Once printed out, the JS will look something like this

alert('foo');
alert('bar');

Whereas with your code, it would've printed it like this

alert(foo);
alert(bar);
于 2013-07-25T15:16:46.940 回答
0

in your php file include load2.php

header("Content-Type: text/javascript");

in the in the top. so your browser get what it wants.

$i=0;
$storeArray = array();

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $storeArray[$i] = $row['DayNum']; 
    $i++;
}
echo "var arr = Array();";
echo "function test() {";
foreach ($storeArray as $key=>$item) {
    echo "arr[".$key."] = ".$item.";";   
}
echo "}";
echo "alert(arr);";

actually you can comment out the two echos containing the <script></script> part when including the file as <script src="load2.php" type="text/javascript" ...

于 2013-07-25T15:23:52.550 回答