passing a value from a checkbox php...

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

aej1973Asked:
Who is Participating?
 
gr8gonzoConnect With a Mentor ConsultantCommented:
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
 
gr8gonzoConsultantCommented:
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
 
aej1973Author Commented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
gr8gonzoConsultantCommented:
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
 
aej1973Author Commented:
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
 
aej1973Author Commented:
ok, just saw your post, let me give it a try...
0
 
gr8gonzoConsultantCommented:
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
 
aej1973Author Commented:
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
 
Ray PaseurCommented:
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
 
aej1973Author Commented:
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
 
aej1973Author Commented:
I have got the answer I was looking for, there was a conflict with the variable names. Thank you for the help.

A
0
 
aej1973Author Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.