Solved

How to set checkbox values for checked or not checked

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Split wordpress loop 35 56
check mysql insert 12 26
Powershell script does not exit 4 24
Add a loading gif while php runs server side 15 28
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

808 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