Scavenger Posted January 19, 2008 Share Posted January 19, 2008 Hey guys, This seems rather simple but as I'm new to php I cannot solve it and have been unable to find a solution anywhere. What I'm trying to do is echo back a value from a php array using a javascript function. But that value is of a specific index in the array which is represented by 'value'. What is the proper way to right this php part? http://www.simplemachines.org/about/copyright.php SMF © 2006, Simple Machines LLC <script> function myFunction(value) { alert ("<?php echo $myArr[value][0]["name"] ?>"); } </script> On a side note, if I replace [value] with 0 the alert returns the value I am looking for so I suspect that there is a way to make this work. Thanks, Scav Quote Link to comment https://forums.phpfreaks.com/topic/86735-passing-a-javascript-var-into-a-php-echo/ Share on other sites More sharing options...
Ken2k7 Posted January 19, 2008 Share Posted January 19, 2008 What does the array look like? Example? Quote Link to comment https://forums.phpfreaks.com/topic/86735-passing-a-javascript-var-into-a-php-echo/#findComment-443266 Share on other sites More sharing options...
resago Posted January 19, 2008 Share Posted January 19, 2008 PHP is server side, javascript is client side, you need to get value back to the server. Most commonly accomplished by a form submitting to itself. or silently send a form via a hidden inline frame and then get the results with javascript in the parent page. Quote Link to comment https://forums.phpfreaks.com/topic/86735-passing-a-javascript-var-into-a-php-echo/#findComment-443273 Share on other sites More sharing options...
resago Posted January 19, 2008 Share Posted January 19, 2008 conversly, you could explode your arrays into javascript arrays, then access them directly. This is assuming the data is not sensitive. Quote Link to comment https://forums.phpfreaks.com/topic/86735-passing-a-javascript-var-into-a-php-echo/#findComment-443298 Share on other sites More sharing options...
Scavenger Posted January 19, 2008 Author Share Posted January 19, 2008 I thought I would avoid recreating the array in javascript. There must be a way to make this work. If alert ("<?php echo $myArr[0][0]["name"] ?>"); returns a value then can't it be adapted to something like alert ("<?php echo $myArr['+value+'][0]["name"] ?>"); .Obviously the latter doesn't work, but there must be a proper way to write this. Quote Link to comment https://forums.phpfreaks.com/topic/86735-passing-a-javascript-var-into-a-php-echo/#findComment-443302 Share on other sites More sharing options...
Aureole Posted January 19, 2008 Share Posted January 19, 2008 $javascript = <<<HTML <script> function myFunction(value) { alert ("{$myArr[value][0]["name"]}"); } </script> HTML; Edit:The forum likes to mess up things I put in php/code tags, but you get what I mean. ...something along these lines may work, you might need to play around with it a little. On a system I'm currently working on I have an include for the header, inside the include I have something like: <script type="text/javascript"> /* <![CDATA[ */ <?php if(isset($page_js) echo $page_js; ?> /* ]]> */ </script> Then on my pages, before the header if I want the page to have some Javascript (just for that one page) I just do... <?php $page_js = <<<HTML $(document).ready(function() { $('a').focus(function() { this.blur(); } ); }); HTML; ?> That works for me, perhaps now you will be able to get whatever you are attempting to work. Quote Link to comment https://forums.phpfreaks.com/topic/86735-passing-a-javascript-var-into-a-php-echo/#findComment-443407 Share on other sites More sharing options...
resago Posted January 19, 2008 Share Posted January 19, 2008 there is, but you have to get 'value' back to the server, so the server can run the php to get the array data for that index. Quote Link to comment https://forums.phpfreaks.com/topic/86735-passing-a-javascript-var-into-a-php-echo/#findComment-443577 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.