Forgive me for asking such a basic question, but I have tried and failed many times trying to figure out what's wrong. I have a very simple html form consisting of one checkbox. I would then like to use jquery to load an external php file onto the same page as the form in the 'content' div, so that the form is clearly updateable. The php file is simply a check to see whether the checkbox was checked and to return the result on screen. Here is the html:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#submitbutton").click(function(){
$("#content").load("loadedpage.php");
return false;
});
});
</script>
</head>
<body>
<form id="testform" action="" method="post">
<input type="checkbox" id="checkbox1" name="checkbox1" value="testcheckbox">Check this box:</input>
<input type="submit" id="submitbutton" name="submitbutton" value="Submit">
</form>
<div id="content">
</div>
</body>
</html>
And here is the external php file:
<?php
if(empty($_POST["checkbox1"]))
{
echo "box wasn't checked";
}
else
{
echo "box was checked";
}
?>
My problem is that no matter whether the checkbox is ticked, the php will return "the box wasn't checked", however, when I have passed this form to the php file through the 'action' attribute everything works as it should, therefore I know the error is probably in the jquery. If anyone could shed some light I would be very grateful.
.load()doesn't pass along any POST params. It sends a GET request for the resource, resulting in$_POSTbeing empty on the server. You need to use$.ajaxinstead, then do something with the server's reply in thesuccessmethod.