Jump to content

PHP variable into javascript


idy

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.
Link to comment
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.
Link to comment
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 !
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.