Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a Clickable Link Using MySql Fetch Array and Ajax
    text
    copied!<p>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:</p> <pre><code>while ($data = mysql_fetch_array($result)) { $link = $data['id']; //example results will be 12345 echo '&lt;span id="val" onclick="goto()"&gt;'.$link.'&lt;/span&gt;'; //lists 12345, and makes each clickable } </code></pre> <p>Part of my Javascript code is as follows:</p> <pre><code>function goto(str) { Ajax... var idval = document.getElementById('val').innerHTML; //grabs 1,2,3,4, or 5 based on which number was clicked Ajax... } </code></pre> <p>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.</p> <p>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.</p> <p>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.</p> <p>I really appreciate anyones help. Your suggestions and hopefully an answer will really help make my php projects much more useful. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload