tags:

views:

103

answers:

3

Hello,

I am using a simple HTML checkbox in a form to put a 1 for checked and a 0 for unchecked in a field called "subcheck" in a MySQL table.

Does the checkbox default to 1 for "checked" and 0 for unchecked? If not, how can I give it those values?

Thanks in advance,

John

Form:

<div class="subcheck"><INPUT TYPE=CHECKBOX NAME="subcheck">Click here to receive free money in the mail.<P></div>

In the file the form goes to:

$subcheck = $_POST['subcheck'];

mysql_query("INSERT INTO submission VALUES (NULL, '$uid', '$title', '$slug', '$cleanurl', '$displayurl', NULL, '$subcheck')");

The MySQL table:

`submission` (
  `submissionid` int(11) unsigned NOT NULL auto_increment,
  `loginid` int(11) NOT NULL,
  `title` varchar(1000) NOT NULL,
  `slug` varchar(1000) NOT NULL,
  `url` varchar(1000) NOT NULL,
  `displayurl` varchar(1000) NOT NULL,
  `datesubmitted` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `subcheck` tinyint(1) NOT NULL,
  PRIMARY KEY  (`submissionid`)
)
+7  A: 

If the checkbox isn't ticked then nothing is sent to the server. Instead, you can provide a default value with:

$subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
Nev Stokes
A: 

If Checkbox is unchecked then $subcheck is not submited at all.

In PHP you should write:

if !(isset($_POST['subcheck']))
  $subcheck = 0;

mysql_query("INSERT INTO submission VALUES (NULL, '$uid', '$title', '$slug', '$cleanurl', '$displayurl', NULL, '$subcheck')");
Ljubomir Đokić
A: 

u should try if statement or ternary condition statement.. if(isset($_POST['subcheck'])) $subcheck = 1; else $subcheck = 0;

or u can try $subcheck = (isset($_POST['subcheck'])) ? 1 : 0;

and in database u should use enum type for subcheck

Tejas1810