leyjen
leyjen

Reputation: 23

Array return checkbox ON in php

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.

Upvotes: 0

Views: 47

Answers (2)

Gabriel Souto
Gabriel Souto

Reputation: 620

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");   

Upvotes: 0

Briley Hooper
Briley Hooper

Reputation: 1271

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.

Upvotes: 1

Related Questions