Solved

passing a value from a checkbox php...

Posted on 2013-01-17
12
366 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 34

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 34

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
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 300 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 34

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 108

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 count occurrences of each item in an array.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now