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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

gr8gonzoConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.