2

hi i have a form which has a checkbox and i want to save the value of the checkbox into my database but when i click on the checkbox and save it it works fine and when i try to save the form without checking on the chekbox it shows undefined index can any one help me out

here is my html

<input type="checkbox" name="active" value="1"></input>

here is my php

$nactive  = $_POST["active"];

here is my save part

mysql_query("INSERT INTO `usermain`( `username`, `password`, level, active,`zimname`, zimmob, `email`, admin, makhtab)
            Values
                   ('$nuser', '$npwd', '$nlevel', '$nactive', '$nzname', '$nzmob', '$nemail', '0', '$makh')") or die(mysql_error());
kumail
  • 59
  • 2
  • 6
  • 16
  • 1
    [Please, don't use mysql_* functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) in new code. They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [_prepared statements_](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – TNK Mar 11 '13 at 15:02
  • thnk u sir will surely learn this – kumail Mar 11 '13 at 15:05

3 Answers3

8

If the checkbox isn't checked, the browser won't actually send the data in your POST request. You'll need to check if the value is set, and then update your variable accordingly.

$inactive = isset($_POST["active"]) ? $_POST["active"] : 0;

mcryan
  • 1,566
  • 10
  • 20
3

If the checkbox is not checked, the value is not posted to the script. You must check if it is set before using it.

if(isset($_POST['active'])){
    //do something if is
}
else{
    //do something if not
}
UnholyRanger
  • 1,977
  • 14
  • 24
0

Use an isset control in the next page like this :

if (!isset($nactive)) $nactive = 0;

If your checkbox isn't checked, the value will be 0

Chris
  • 156
  • 5