?
Solved

passing a value from a checkbox php...

Posted on 2013-01-17
12
Medium Priority
?
374 Views
Last Modified: 2013-01-18
Hi, I have an input box that has a checkbox checked depending on the value that is there in the DB. So if the value of $local==1 the checkbox is going to be checked. I am having a problem updating this form. When I uncheck this box and submit the form, the value still remains as 1. What do I do  have the correct value ( 0 when unchecked ) be passed when the form is submitted?

 Thank you.
<td class=""><input type="checkbox" name="local" id="local" <?php if ($local == 1) echo 'checked'; ?>  value=""  ></td>

Open in new window

//This is the code I use when I submit the form.

 if(isset($_POST['local']))
        {$local=1;
             }else
         $local=0;

Open in new window

0
Comment
Question by:aej1973
  • 7
  • 4
12 Comments
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38789217
My guess is that when you re-submit the form, you have something else that is coming across as an input named "local". Try adding this before you check your $_POST values:

print_r($_POST);
0
 

Author Comment

by:aej1973
ID: 38789277
What I am doing is passing the value through a jquery Ajax script as follows;

 $("#ph-update").click(function() {
       .....
          var local= $("#local").val();

   ......
    if(profileid!=" ") {

             $.ajax({//Make the Ajax Request
                cache: false,
                type: "POST",
                url: "db_profile_edit.php",
                data: "profileid="+ profileid+"&local="+local,
                beforeSend:  function() {...


Is there something I will need to do differently here? Thanks for the help.

A
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38789422
Ah, that's a jQuery problem. I think the val() of a checkbox only looks at the value=1 attribute - it doesn't look at whether or not something is checked.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Accepted Solution

by:
gr8gonzo earned 1200 total points
ID: 38789435
Try this:
var local = $('#local').is(':checked') ? $("#local").val() : 0;

And then in your PHP code:
if( isset($_POST['local']) )
  $local = intval($_POST['local']);
0
 

Author Comment

by:aej1973
ID: 38789438
yes, that is right. So I change it as follows;

var local= $('#local:checked').val()

So now what happens is that when the box is checked the correct value is passed but when the box is not checked I get an output of "undefined". Not sure how to handle that.

A
0
 

Author Comment

by:aej1973
ID: 38789445
ok, just saw your post, let me give it a try...
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38789449
Yes, because if it's not checked, then the selector #local:checked is not matching up to any elements, so you can't get the val() of an element that is not found. Try the code I suggested.
0
 

Author Comment

by:aej1973
ID: 38789497
In either case ( checked or unchecked )the output I get now is 0. My code snippet is attached;
<td class=""><input type="checkbox" name="local" id="local" <?php if ($local == 1) echo 'checked'; ?>  value="1"  ></td>

Open in new window

 var local = $('#local').is(':checked') ? $("#local").val() : 0;

Open in new window

if( isset($_POST['local']) )
     $local = intval($_POST['local']);

Open in new window


Thank you for the help.

A
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38789842
You might want to try to get the backend script right, then add the jQuery layer.  If you have any doubt about how to always have a value for an input control, this article teaches one way that I've used satisfactorily.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_5450-Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html
0
 

Author Comment

by:aej1973
ID: 38790258
This code is really bugging me.  Can someone take a look at this script and let me know where I am making a mistake.  All I am trying to do is to see if a check box is clicked, if it is the value returned should be 1 if not 5. What I cannot understand is why this logic is only working for var intl but for the other two variables the value returned is always 5. Can someone help, thank you.
0
 

Author Comment

by:aej1973
ID: 38793976
I have got the answer I was looking for, there was a conflict with the variable names. Thank you for the help.

A
0
 

Author Closing Comment

by:aej1973
ID: 38793978
Thank you.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 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