Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing variables from JQuery to PHP using $.post
    primarykey
    data
    text
    <p>I have a form (#searchform) that I want to submit multiple times without reloading the page, so I use jQuery and the post-method. I want to store which checkboxes are checked in a variable named <em>target</em> (array, list or whatever) and which radio button is selected in the variable <em>diff</em>. </p> <p>The variables also need to be available in PHP. I tried passing them as <em>ftar</em> and <em>fdiff</em> in so many ways, but nothing worked.</p> <p>Any help is much appreciated! </p> <p>JQuery in submit.js:</p> <pre><code>$(document).ready(function () { var target = new Array(); $('#searchform input:checked').each(function() { target.push($(this).attr('value')); }); var diff = $('#searchform').find("input[class='diff']").val(); $.post('index.php', {ftar: target, fdiff: diff}); }); </code></pre> <p>HTML and PHP in index.php:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang="en-US"&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="js/submit.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;?php ini_set('display_errors',1); error_reporting(E_ALL); $link = new mysqli("localhost", "root", "password", "mytable"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $target = $_POST['ftar']; $diff = $_POST['fdiff']; ?&gt; &lt;div id="right"&gt; &lt;div class="content"&gt; &lt;form id="searchform" method="POST" action="/"&gt; &lt;input type="checkbox" value="Check1"&gt; &lt;input type="checkbox" value="Check2"&gt; &lt;input type="checkbox" value="Check3"&gt; &lt;input type="radio" class="diff" value="Radio1"&gt; &lt;input type="radio" class="diff" value="Radio2"&gt; &lt;/form&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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