Forum Moderators: open

Message Too Old, No Replies

Problem with javascript + php

document.write

         

Hevymetal

11:48 am on Sep 3, 2004 (gmt 0)

10+ Year Member



Hi ... i use javascript and php. I don't understand what is wrong with this code:

<html>

<head>
<title>test</title>
</head>

<body>
<?php
$t=array();
$t[]="zero";
$t[]="one";
?>

<script language="JavaScript"><!--
var message=<?php echo $t[0];?>;
document.write(message);
//-->
</script>

</body>

</html>


It should print "zero" but it does not.

Bernard Marx

12:37 pm on Sep 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What does it do then?
I guess you're getting this:

<script language="JavaScript">
var message=zero // need message="zero"
document.write(message);
</script>

You'll need to put in the extra quotes in the php,
- which makes it off-topic here :)

Hevymetal

2:19 pm on Sep 3, 2004 (gmt 0)

10+ Year Member



mhh ... didn't understand know. What it does ... it just is blank ... doesn't write anything.

What i want to do is not to write "zero", but rather get values from php array and use it with javascript code.

Perhaps you could fix my javascript to read the value from $t[0] and then print it.

thnx

Bernard Marx

3:33 pm on Sep 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps you could fix my javascript to read the value from $t[0] and then print it.

If you want a Javascript variable that holds the string, "zero", then you need to get PHP to make sure that, this (including the quotes):

var mystring = "zero"

appears, somewhere within <script> tags, on the page, as received by the client.
That is your department (PHP).

StupidScript

4:40 pm on Sep 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add quotes around the JS variable value:

<script language="JavaScript"><!--
var message="<?php echo $t[0];?>";
document.write(message);
//-->
</script>

The PHP quotes indicate the value of the string to pass. When JS gets it, it looks more like a variable reference than a string to JS. Adding the quotes in the JS, as above, lets the script know the value is, indeed a string.

Hevymetal

7:07 pm on Sep 3, 2004 (gmt 0)

10+ Year Member



oh .. sry ... didn't know that quotes mean "".

ok thnx ... now i know.

Hevymetal

7:53 pm on Sep 3, 2004 (gmt 0)

10+ Year Member



ok another problem with quite the same example.

I want to get access to php array[$i] in javascript for loop.
Here is the code to illustrate the problem:


<body>
<?php
$t=array();
$t[]="zero";
$t[]="one";
?>

<script language="JavaScript"><!--
for(i=0; i<2; i++){
var message='<?php echo $t[i];?'>; //here is error
document.write(message);
}
//-->
</script>

</body>

StupidScript

8:05 pm on Sep 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here ya go (note the exact placement of the quotes):

<script language="JavaScript"><!--
for(i=0; i<=1; i++){
var message="<?php echo $t[i];?>"; //quotes outside PHP
document.write(message);
}
//-->
</script>

But that won't work, because it's a PHP array, and you are incrementing a Javascript variable. The solution is to use PHP to write the Javascript, like this:

<script language="JavaScript"><!--
<?php
for($i=0; $i<sizeof($t); $i++){
$message=$t[$i];
print("document.write(\"$message\");");
}
?>
//-->
</script>

Of course, in this case there is no need to use Javascript to write the array elements, because PHP can handle it just fine:

<?php
for($i=0; $i<sizeof($t); $i++){
$message=$t[$i];
print("$message<br />\n");
}
?>

Have fun, and you are encouraged to check out the PHP forum.

Hevymetal

8:20 am on Sep 4, 2004 (gmt 0)

10+ Year Member



Thnx ... now it works.