?
Solved

passing a value from a checkbox php...

Posted on 2013-01-17
12
Medium Priority
?
371 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
[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
  • 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

762 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