Solved

How to set checkbox values for checked or not checked

Posted on 2016-11-05
3
26 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 49

Assisted Solution

by:Ryan Chong
Ryan Chong earned 50 total points
Comment Utility
in php, you can try this:

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

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 450 total points
Comment Utility
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
Comment Utility
I really like the simple way of using a hidden field. That worked like a charm :)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
using web browser with BING 40 83
Modify Table Width 6 13
Glyph icons in Bootstrap 3 4 9
Help cleaning out CSS 2 21
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now