ROY2 Posted May 14, 2013 Share Posted May 14, 2013 Hi, I am trying to get this code to work, and can't figure out what I'm not doing correctly. New to javascript, PHP and HTML. My PHP code below is not working. I select records from some data base files and store all of the records in an array. I display the information from the first record on the screen. When the "Next" button on the screen is clicked, I want to read the array and get the next record and display that information on the screen. The user will continue to hit the "Next" button until all the records have been displayed on the screen for review. I've tried to do this various ways and just cannot get it to work, and this is my latest version. I didn't know how to get the $item_array code to work within the javascript code.So now I am trying to test the VAR mytest from the javascript. Do I need to use AJAX to pass it or is there a better way? Or how do I get the code to work in javascript? Any help would be appreciated!! Regards, Roy My array definition: $item_array = array(); Sample data from array: [10009] => Array ( [iTEM] => 10009) [10063] => Array ( [iTEM] => 10063) [10066] => Array ( [iTEM] => 10066) Loading the array from the file: $item_array[$item] = $row; When the "Next" button on screen is clicked, I execute a javascript function, passing a value of 1.The javascript code is executed and mytest = 1 My html code: <input type="button" class="button" value="Next Item" onclick="javascript:setval(1)" /> My script: <script> var mytest = ''; function setval(varval) { mytest= varval; } </script> In PHP code, I'm testing if mytest = 1, then <?php $mytest = mytest; if (mytest == '1') { foreach ($item_array as $key => $value) { $itemkey = $key; echo $itemkey; } } ?> Link to comment Share on other sites More sharing options...
HartleySan Posted May 14, 2013 Share Posted May 14, 2013 ROY2, I honestly have no clue what your code is trying to execute, but here are some general pointers: - If you aren't using JS, then you'll have to re-query the DB each time the Next button is clicked, because the page will have to be reloaded. For this to work, you're going to have to either use a session/cookie, or you're going to have to send some sort of parameter through the URL for the Next button to know which record you need from the DB. - If you're using JS, you can either use Ajax, which would require something similar to the above with Ajax tacked on as well, or you could get all the required info in one query when the page first loads, load it into a JS object or whatever, and then use that JS object to update the DOM when the Next button is clicked according to some sort of counter variable you're using in JS. Link to comment Share on other sites More sharing options...
ROY2 Posted May 15, 2013 Author Share Posted May 15, 2013 Thanks for answering, it helped clear things up for me. Link to comment Share on other sites More sharing options...
HartleySan Posted May 15, 2013 Share Posted May 15, 2013 Glad to here it. Please share your solution when you come across it. Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts