Solved

How to set checkbox values for checked or not checked

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 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