Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to do it in pure javascript,<br> then pass <code>this</code> to your <code>ajax_answer</code> function from <code>onclick</code> event like below </p> <pre><code>&lt;?php while( $u = mysql_fetch_array( $result ) ) { ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $u['question_id'];?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $u['question'];?&gt;&lt;/td&gt; &lt;td&gt; &lt;input type="hidden" value="&lt;?php echo $u['question_id'];?&gt;" /&gt; &lt;input type="text" /&gt; &lt;a href="#" onclick="ajax_answer( this );"&gt;Send Answer&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; </code></pre> <p>and your javascript will be... </p> <pre><code>&lt;script type="text/javascript"&gt; function ajax_answer( elem ) { var question_id = elem.parentElement.children[0].value; var answer = elem.parentElement.children[1].value; /// do your request here return false; } &lt;/script&gt; </code></pre> <p>And the same with jQuery.<br> Add <code>name</code> attribute to those input elements, and add a <code>classname</code> to <code>anchor</code> element</p> <pre><code>&lt;?php while( $u = mysql_fetch_array( $result ) ) { ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $u['question_id'];?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $u['question'];?&gt;&lt;/td&gt; &lt;td&gt; &lt;input type="hidden" name="question_id" value="&lt;?php echo $u['question_id'];?&gt;" /&gt; &lt;input type="text" name="answer" /&gt; &lt;a href="#" class="send_answer"&gt;Send Answer&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; </code></pre> <p><strong>Javascript</strong><br> add the <code>onclick</code> event handler dynamically. Now your function <code>ajax_answer</code> accepts two parameters <code>question_id</code> and <code>answer</code>, we will pass those two parameters through the <code>click</code> event handler</p> <pre><code>&lt;script type="text/javascript"&gt; $(function() { $("a.send_answer").on("click", function( event ) { event.preventDefault(); var td = $(this).parents("td:first"); var qid = $("input[name=question_id]", td).val(); var ans = $("input[name=answer]", td).val(); ajax_answer( qid, ans ); }); }); function ajax_answer( question_id, answer ) { /// do your request here } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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