Jump to content

PHP Checkbox Echo - The Solution Must Be Simple - I Need Help Finding It


surge42

Recommended Posts

Guys,

 

I'm a newbie.   There, I got that out of the way....

 

I have a form that can be submitted and then modified.  The data gets sent to a mySQL db.  It's nothing special.

 

The idea is that the user is to click a checkbox located at the bottom of the form that says "I'm finished with my project" when checked the stored value is "yes".

 

Everything works fine the value gets stored and all is well except I can not get the darn thing to echo the chekced value.

 

Below is what I have tried with no joy.

 

<input type="checkbox" name="projectcomplete[]" <?php if ($user_type_detail["projectcomplete"]=='Yes') {echo "checked";} ?>>

Why the heck won't it work?  The value "yes" is stored.  Arrrggggg.... 

 

I also tried isset with no joy...

 

<input type="checkbox" name="projectcomplete" <?php if (isset($yes)) { echo 'value="checked"'; }?> >

 

The following code below works perfectly on the form.  I have refferenced it but no solution materialized.    What can I tell ya.. I'm a newb... I don't have the skills to uncover the solution.

 

input type="radio" class="radio" name="CYSD_Particiapant" value="Yes" <?php if($user_type_detail["CYSD_Particiapant"]=='Yes') echo 'checked=checked';else echo '';?> />Yes<input class="radio" type="radio" name="CYSD_Particiapant" value="No" <?php if($user_type_detail["CYSD_Particiapant"]=='No') echo 'checked=checked';else echo '';?> />No

Any help would be very much appriated.

Link to comment
Share on other sites

OK, let's take one step at a time.

 

 

 

<input type="checkbox" name="projectcomplete[]" <?php if ($user_type_detail["projectcomplete"]=='Yes') {echo "checked";} ?>>

Why the heck won't it work?  The value "yes" is stored.  Arrrggggg.... 

 

 OK, how do you know it is stored? Have you verified the value through PHPMyAdmin? Have you echo'ed $user_type_detail["projectcomplete"] to the page to verify it's contents? have you verified that the field name is correct (you state the last example works but it has a typo in the field name)? Are you sure the value is exactly "Yes" and not "yes" or "YES"?

 

Don't assume what variables contain because you think you know what they should contain. Test Them! Echo them to the page or write some code to test them. That is the basics of debugging.

 

Also, I would highly suggest NOT using "Yes" or "No" for a field value. instead make the field an INT and store 0/1 which can be logically interpreted as TRUE and FALSE. Then you don't nee to worry about putting a typo in your comparisons such as

 

if($var == 'Yes')

 

Instead you can test the variable directly

 

if($var)

which will be true is $var is a 1 and false if $var is 0

Link to comment
Share on other sites

In your third (working example) you have no square brackets after the variable name - it is a variable not an array (which is what you want) and you use checked=checked which will work.

 

In example 2 you use value=checked -- won't do what you want

 

In example 1 you echo just checked (which will work but is deprecated) but have [ ] after the variable name making it an array and I suspect you don't process it as an array.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.