idy Posted September 13, 2006 Share Posted September 13, 2006 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 https://forums.phpfreaks.com/topic/20601-php-variable-into-javascript/ Share on other sites More sharing options...
Gruzin Posted September 13, 2006 Share Posted September 13, 2006 [code]$array[' + id + '];[/code] What's this meant to be? Link to comment https://forums.phpfreaks.com/topic/20601-php-variable-into-javascript/#findComment-90942 Share on other sites More sharing options...
ToonMariner Posted September 13, 2006 Share Posted September 13, 2006 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 https://forums.phpfreaks.com/topic/20601-php-variable-into-javascript/#findComment-90943 Share on other sites More sharing options...
idy Posted September 13, 2006 Author Share Posted September 13, 2006 OK thanks a lot. Link to comment https://forums.phpfreaks.com/topic/20601-php-variable-into-javascript/#findComment-90945 Share on other sites More sharing options...
Gruzin Posted September 13, 2006 Share Posted September 13, 2006 You could use this to get the point:[code]<?php$id = "5"; echo "<a href='#' onClick=\"alert ('$id');\">Get it</a>";?>[/code] Link to comment https://forums.phpfreaks.com/topic/20601-php-variable-into-javascript/#findComment-90946 Share on other sites More sharing options...
idy Posted September 13, 2006 Author Share Posted September 13, 2006 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 https://forums.phpfreaks.com/topic/20601-php-variable-into-javascript/#findComment-90950 Share on other sites More sharing options...
kenrbnsn Posted September 13, 2006 Share Posted September 13, 2006 For that you need to look at AJAX.Ken Link to comment https://forums.phpfreaks.com/topic/20601-php-variable-into-javascript/#findComment-90956 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.