I made a form for a website where user post their name and links of their favorite websites. I also want this with no page load every time user submit the form. I want is when user submit the form he or she must accept or read the terms and conditions for using the service. I added a checkbox and google it how to pass the data for validation server side or how to submit the form without loading the page. I came across a very nice tutorial on youtube where he writes html php and ajax code. I implement his code and see that all the data received on server side accept the checkbox. I tried manipulate the code little on server side in php but nothing works. When i tried without Ajax it works. I always received the checkbox ON even when i didn't checked it..Any help ?
html:
<form action="checkbox1.php" method="post">
<input name="name" id="name" type="text"/><br/>
<input type="text" id="name2" name="name2"><br/>
<input type="checkbox" id="checkbox" name="checkbox"><br/>
<input type="submit" id="submit" name="submit">
</form>
Ajax:
$(document).ready(function() {
$("form").submit(function(event){
event.preventDefault();
var name = $("#name").val();
var name2 = $("#name2").val();
var checkbox = $("#checkbox").val();
var submit = $("#submit").val();
$(".text-danger").load("checkbox1.php", {
name: name,
name2: name2,
checkbox: checkbox,
submit: submit
});
});
});
php:
<?php
print_r($_POST);
if (isset($_POST["submit"])) {
$name = $_POST["name"];
$name2 = $_POST["name2"];
$checkbox = $_POST["checkbox"];
$nameerror = false;
$name2error = false;
$checkboxerror = false;
$errorEmpty = false;
if (empty($_POST["name"]) || empty($_POST["name2"])) {
echo "There is something wrong with your error";
$errorEmpty = true;
} elseif (empty($_POST["checkbox"])) {
echo "Please check the box";
$errorEmpty= true;
} else {
echo "Thanks!";
}
}
?>
Result: Array ( [name] => Mark [name2] => Otto [checkbox] => on )
I am sorry if this question out of tags because i don't know it is server side issue or ajax code problem so i added all categories.