Jump to content

Checkbox problem


Recommended Posts

Hi.

 

I have a form that contains a checkbox.

 


<form action="add_info_handler.php" method="post" >
   <table>
      <tr>
         <td align="right">Add Info</td>
         <td align="left"><input type="checkbox" name="add_info_approved" value="yes" /></td>
         <td><input type="submit" name="add_info_button" value="Add New Info" /></td>
      </tr>
   </table>
</form>

 

The add_info_handler.php script obtains the value as follows:

 

$_add_info_value = $_REQUEST['add_info_approved'];
if ($_add_info_value == "yes"){
		$_info_check = "Y";
	}else{
		$_info_check = "N";
	}

 

I want to then place this value into a table in MySQL:

 


$q = "INSERT INTO info_table (add_info_check) VALUES ($_info_check)";
$r = @mysqli_query ($dbc, $q);

 

However, this does not seem to boe working, even though the query is correct and the fields match those in the table. I have printed out $_info_check and that contains the correct value.

 

Is there something else I should be doing with the check box? Or is is because I am using REQUEST? I have tried to use post but this does not work either.

 

There are other values I have ommited which are placed into the table when the value above is not placed into the query so I know I have the connection etc. to the DB.

 

Thanks.

 

MB

Link to comment
Share on other sites

I would also like to add some suggestions.

 

You are storing a boolean value - one which is either true or false. It is standard procedure to store the value as either 0 (false) or 1 (true). All programming languages (at least those that I am aware of) will inherently interprest a 1 and 0 as true and false. So, if you do a comparison, you can do this

if($info_check)

instead of this

if($info_check=='yes')

This makes your code much for efficient.

 

Also, the value of a checkbox in a form field is hidden from the user. So there is no reason to make the values yes/no and then change it on the processing page. Simply make the values of the checkboxes the values you will use in the processing page. But, don't use that as an excuse not to ensure that malicious code is not being submitted. Assuming the value of the checkbox is '1', my processing code would do something like this

$value = ($_POST['field_name']=='1');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.