0

I cant seem to get my php script to recognize that the checkbox in my form is checked.

I want to accomplish this:

If checkbox is checked, the php script should submit to my DB.

PHP:

<?php
if (!empty($_POST['approve_student'])) {

  if (isset($_POST['approve'])) {

        //submit

      } else {

        //do nothing
    }
  }
?>

FORM:

<input class="checkbox" name="approve" type="checkbox" id="approve">  

<label name="approve" for="approve"><span><div data-textbox="1" ></div></span></label>

<input  class='button_submit_2' name="approve_student" type="submit" value='Submit'>

NOTES:

  • Im using Jquery to keep checkbox checked during session.
  • Im using CSS to customize checkbox.
  • I dont know if these two might corrupt anything.
  • Hope u can help.
Sébastien
  • 11,860
  • 11
  • 58
  • 78
Pjust
  • 81
  • 1
  • 3
  • 15
  • It actually works for me. Are you sure that the error might not be in the !empty($_POST['approve_student']) ? try to take the isset($_POST['approve']) outside it and check if it works! – briosheje Oct 09 '13 at 10:07

2 Answers2

0

If the checkbox isn't checked then the value shouldn't be coming through in the first place, so this should work;

if(isset($_POST["testvariabel"])){
   //whatever you wanna do here
}

or if it's always coming through...

Change the markup to something like this, setting a value property

<input type="checkbox" class='form' onclick="this.value=!this.value" value=true name="checkbox[]" /> and to get the values submitted, use a simple loop

if($_POST['checkbox'] == 0){
    echo $checkbox . ' ';
}
pythonian29033
  • 5,148
  • 5
  • 31
  • 56
0

Your code is fine it is working

if (!empty($_POST['approve_student'])) {

    if (isset($_POST['approve'])) {

        echo "Approved";

    } else {

        echo "Not Approved";

    }
}

<form action="index.php" method="post">
<input class="checkbox" name="approve" type="checkbox" id="approve">  
<label name="approve" for="approve"><span><div data-textbox="1" ></div></span></label>
<input  class='button_submit_2' name="approve_student" type="submit" value='Submit'>
</form>
Haver
  • 443
  • 2
  • 11
  • Well, this is embarrising. . Forgot method="post" in my form! Maybe i should call it a day. Anyways thanks for the quick fix :) – Pjust Oct 09 '13 at 10:22