Jump to content

Submit check-box value to MySQL


paymentstv

Recommended Posts

Hello All,

 

I am new to PHP and I am trying to modify a already written script since I want to add a checkbox to my site.

In the front end I have

<input type="checkbox" name="privateurlcheck" id="privateurlcheck" value="0">

 

in the php I have

var privateurlcheck = document.getElementById( "privateurlcheck" ).value

 

Then in the mysql data insert php page,

$records[channel_protected] = $postData[privateurlcheck] ;

 

In MySQL "channel_protected" field is "ENUM" with Values '0','1'

 

When I run the code I see 0 in the MySQL channel_protected field even when I check the checkbox. All other values that are passed on to mysql such as name, age are posted correctly without any issues.

 

 

Can you please let me know how can I get 0 or 1 in the mysql depening on the checkbox status?

 

It is 5AM and I am trying to figure this for 2nd consecutive day! Greatly appreciate if any one can help me out.

 

Link to comment
https://forums.phpfreaks.com/topic/221308-submit-check-box-value-to-mysql/
Share on other sites

If a checkbox is ticked when the page is submitted it will have a value of "on", which is not an acceptable entry in your database. You need to change this to 1.

 

$records[channel_protected] = ($postData[privateurlcheck]) ? 1 : 0;

 

So if $postData[privateurlcheck] evaluates to true, it will set $records[channel_protected] to 1. if not, it will be set to 0.

 

"on" evaluates to true. "" evaluates to false.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.