0

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.

4
  • you should be checking in the js not the php Commented Nov 7, 2017 at 23:42
  • you mean ajax code .? can tell me whats wrong with this code .? Commented Nov 7, 2017 at 23:44
  • the user wont see the output from the php as its an AJAX call Commented Nov 7, 2017 at 23:45
  • for output i use simple javascript to pass the same data from server side like "Please check the box".. IT WORKS except the checkbox Commented Nov 7, 2017 at 23:49

2 Answers 2

1

You're always getting a value for the checkbox in PHP because of this line in your javascript:

var checkbox  = $("#checkbox").val();

That line gets the "value" of the checkbox element, whether or not it's checked. You could instead do something like this:

var checkbox = $("#checkbox").is(":checked") ? 'on' : '';

That would send a value of on if the checkbox is checked, and an empty string if the checkbox is not checked.

Sign up to request clarification or add additional context in comments.

1 Comment

wooooo !!! Thank you soo much... I was searching hours and here it is .. Thanks it works
0

Getting the value of the checkbox will always return 'on'. You should try get the property checked instead of value.

In your Ajax code, try this:

$("#checkbox").prop("checked");   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.