Solved

How to set checkbox values for checked or not checked

Posted on 2016-11-05
3
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 52

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 110

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
 
LVL 1

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Find out what you should include to make the best professional email signature for your organization.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

690 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