I am trying to use specific numeric results from a mysql_fetch_array within a while loop to execute a Javascript function (Ajax). A basic snippet of my php code is as follows:
while ($data = mysql_fetch_array($result))
{
$link = $data['id']; //example results will be 12345
echo '<span id="val" onclick="goto()">'.$link.'</span>'; //lists 12345, and makes each clickable
}
Part of my Javascript code is as follows:
function goto(str)
{
Ajax...
var idval = document.getElementById('val').innerHTML; //grabs 1,2,3,4, or 5 based on which number was clicked
Ajax...
}
What I am trying to do here is query multiple records from MySql, list the results using a while loop, make each result clickable, and pass the inner html to a variable for Ajax.
My problem is that when I click any of the results from the while loop list, I pass only the first result of the mysql fetch array. For example, the while loop will list 12345. when I try clicking 3, I get 1 as the inner html; when I try clicking 5, I get 1 as the inner html; and so on… It always gets the first result, which is 1.
I tried imploding and exploding the mysql fetch array, however the inner html always ends up being the first row of the array results. I need to break up the array somehow, so that each row is independent, not just visually, but programmatically so I can get it using getElementById.innerHTML.
I really appreciate anyones help. Your suggestions and hopefully an answer will really help make my php projects much more useful.
First, change your php script to look like this:
Then, use the following javascript code:
But I am not sure about what
strmeans in your javascript code and why functiongototakes 1 arguments (str) while is called with no arguments (onclick="goto()").