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 target (array, list or whatever) and which radio button is selected in the variable diff.
The variables also need to be available in PHP. I tried passing them as ftar and fdiff in so many ways, but nothing worked.
Any help is much appreciated!
JQuery in submit.js:
$(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});
});
HTML and PHP in index.php:
<!DOCTYPE html>
<html lang="en-US">
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="js/submit.js"></script>
</head>
<body>
<?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'];
?>
<div id="right">
<div class="content">
<form id="searchform" method="POST" action="/">
<input type="checkbox" value="Check1">
<input type="checkbox" value="Check2">
<input type="checkbox" value="Check3">
<input type="radio" class="diff" value="Radio1">
<input type="radio" class="diff" value="Radio2">
</form>
</div>
</div>
</body>
</html>