Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send a variable from a PHP loop to Javascript
    text
    copied!<p>I have a loop that looks like this:</p> <pre><code> &lt;? $i=1; while ($u=mysql_fetch_array($result1)){ ?&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;Sport&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $u['sport_id'];?&gt;&lt;/td&gt; &lt;td&gt; &lt;a id="modal_window" href="#" rel="open"&gt;Make Question&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php $i++; } ?&gt; &lt;/table&gt; </code></pre> <p>The following modal window is opened when someone clicks on "Make Question":</p> <pre><code> &lt;div id="mascara"&gt;&lt;/div&gt; &lt;div id="ventana"&gt; &lt;a class="ventanatitulo" href="#" rel="close"&gt;Close Window&lt;/a&gt; &lt;h2&gt;Write your question:&lt;/h2&gt;&lt;br/&gt; &lt;form&gt; &lt;input type="text" id="question"/&gt; &lt;br /&gt; &lt;/form&gt; &lt;a href="#" onClick="ajax_question();"/&gt;Send question&lt;/a&gt; &lt;/div&gt; </code></pre> <p>What I want to do, is that every time someone clicks on "Make Question", capture into a variable the question that was written on the modal window (no problem to do this), and the sport_id that was clicked (I DON´T KNOW HOW TO DO THIS!!)</p> <p>This is the code for the modal window:</p> <pre><code>$(document).ready(function(e) { $('a[rel="open"]').click(function(e) { e.preventDefault(); var ancho = $(window).width(); var alto = $(document).height(); var mascara = $("#mascara"); var ventana = $("#ventana"); mascara.css({ "width" : ancho, "height" : alto }); mascara.fadeIn("fast", function() { ventana.css({ "top" : (alto / 3) - (ventana.height() / 2), "left" : (ancho / 2) - (ventana.width() / 2) }); ventana.fadeIn("fast"); }); }); $("a[rel='close']").click(function(e) { e.preventDefault(); $("#ventana").fadeOut("fast", function() { $("#mascara").fadeOut("fast"); }); }); }); </code></pre> <p>And finally this is the code where I want to capture all the variables and send them to page1.php:</p> <pre><code>function ajax_question(){ var question=$('#question').val(); //WRITTED QUESTION IS CAPTURED WITHOUT PROBLEM! var sport_id= //HOW DO I CAPTURE THE SPORT_ID THAT WAS CLICKED? $.ajax({ url:'page1.php', type:'POST', dataType:'text/html', data:'question='+question + '&amp;sport_id='+sport_id, success: function(return){ some code... } }); } </code></pre> <p>I hope you understand what I'm trying to do. Thanks in advance to anyone who can help me!</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