PDA

View Full Version : Javascript and php



icb01co2
08-09-2004, 08:06 AM
Hello there.

I have set a javascript array a[], each array element holds a list of training centres i.e.

a[0] = leeds-edinburgh-leeds-leeds.

i want to split these centres into javascript variuables i.e.

echo "<script>
var aims_array = a[$i].split("-");
</script>
";

but for some unknown reaseon when i add this code to my .php script i get random 0's printed to the screen
i.e.

00000000000000000000
00000000
000000000000000
00
0000000000000
000000

i'm not printing anything to the screen so why should this happen?

Thanks, Chris.

Easywebdev
08-09-2004, 10:25 AM
echo "<script>
var aims_array = a[$i].split("-");
</script>
";

Your problem is the use of double quotes in the echo statement as it encounters another set inside the holding quotes.

Either escape the inner quotes ala split(\"-\"); or use single quotes around the echo ala
echo '<script> var aims_array = a[$i].split("-"); </script>';

Some things to be aware of using ' and " in php.
Everything inside " " (double quotes) is parsed - echo " $some_variable " will output the value of $some_variable.

Nothing inside ' ' (single quotes) is parsed - echo ' $some_variable ' will do just that, print the text $some_variable.

Hope that helps.