Solved

How to set checkbox values for checked or not checked

Posted on 2016-11-05
3
48 Views
Last Modified: 2016-11-05
I have  a checkbox with a value of "yes" if it is checked. It inserts fine into the database if I actually check the checkbox. If I don't, then I get a php error.

Can you use html to have a value for checked and not checked or would I have to use php and do something like, if it isn't empty then it = yes or if it is empty then it = no.

before I write the php, I was just wondering if there was a simpler way just using html?

<input type="checkbox" data-toggle="checkbox" name="featured" value="yes"> Featured product </label>

Open in new window

0
Comment
Question by:Black Sulfur
3 Comments
 
LVL 50

Assisted Solution

by:Ryan Chong
Ryan Chong earned 50 total points
ID: 41875384
in php, you can try this:

$featured= (isset($_POST['featured']))?"Yes":"No";

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 450 total points
ID: 41875392
You can use a hidden input in HTML to set a default value for a checkbox.

Full explanation and code examples here:
https://www.experts-exchange.com/articles/5450/Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html

See especially Another Take on Unchecked Checkboxes.  This works in PHP because of the way that raw post data is presented to the PHP script in the $_POST associative array.  If you were dealing with raw post data in another language, you would need to do what PHP already does for us -- put the post data elements into the $_POST array, using the key/value pairs and overwriting the values for repeated identical keys.
0
 

Author Comment

by:Black Sulfur
ID: 41875409
I really like the simple way of using a hidden field. That worked like a charm :)
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question