Jump to content

Archived

This topic is now archived and is closed to further replies.

idy

PHP variable into javascript

Recommended Posts

Hello !

I have the following JS function

[code]function test(id) {
var result = '<? echo $array[' + id + ']; ?>';
alert(result);
}[/code]

which does not send anything. How is it possible to send the javascript variable id to the PHP variable (array) $array ?

Thanks a lot.

Share this post


Link to post
Share on other sites
you are mixing js and php there buddy!

PHP will stop running once the page has been served to the client - php can do no more after that until the next request is sent.

you wil have to populate and array explicitly in js usign the php array you have constructed and then use id to traverse it.

Share this post


Link to post
Share on other sites
You could use this to get the point:

[code]<?php
$id = "5";
echo "<a href='#' onClick=\"alert ('$id');\">Get it</a>";
?>[/code]

Share this post


Link to post
Share on other sites
Actually the variable $array is already used in my PHP script before writing the JS function.
If I write :
[code]
var result = '<? echo $array[0]; ?>';
[/code]
I do have the content of $array[0] into result.
The only thing I am trying to do is to retrieve the JS variable id instead of the hard-coded value 0 into $array. Or is still the same problem ? Thanks !

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.