1

Even when the checkbox is unselected it is still returning the Value. I am passing the form through an Ajax request and printing the returned value, but it is the same result checked or unchecked. Isset not working properly either.

<input type="checkbox" value="Agree" id="siteAgreement">

if(!isset($siteAgreement) || !$siteAgreement || $siteAgreement != "Agree"){
//////Unchecked
}
1
  • I am posting the data via javascript: "siteAgreement = $("siteAgreement").value;" So I think the problem is that is it always going to get the value from checkbox. Is there a way to change the value oncheck? Commented Jan 11, 2013 at 19:06

4 Answers 4

3

Change

siteAgreement = $("#siteAgreement").value;

to something like this:

siteAgreement = $("#siteAgreement").is(':checked') ? $("#siteAgreement").val() : null;

But you should also add the name attribute so browsers with javascript turned off can also use your site.

Also you can use some code that generates request automatically from form (something like jquery.form), so you don't have to update javascript whenever you change the form.

Example here: http://jsfiddle.net/RhasK/

Sign up to request clarification or add additional context in comments.

2 Comments

Although, now when it is checked it wont execute the Javascript
Won't execute at all? Do you have current version of jQuery?
0

Here try this:

 if(!isset($_POST['siteAgreement']) || !$_POST['siteAgreement'] || $_POST['siteAgreement'] != "Agree"){
 //////Unchecked
 }

Unless you're extracting the variable before, $siteAgreement won't be set. You can use $_POST['nameofelement'] to access the data.

Comments

0
if($_POST["siteAgreement"] !== "Agree") {
//not checked
}

will work fine

1 Comment

But the problem is when I submit even unchecked it's still returning checked
0

you should only need to check if it is set or not.

if(!isset($_POST['siteAgreement'])){
//then it is unchecked
}

Also, you didn't give your input a name. You need to put...

<input type="checkbox" name="siteAgreement" value="Agree" id="siteAgreement">

You need a name to access it in PHP.

2 Comments

Name didn't help. I pull the data via Javascript/ Ajax and pull element values by id
The only way you can get form values from PHP is by setting the name attribute of your input. Did you also try the php code that I put above? Are you sure that the input tag is inside of the form that you're submitting? This is the way that I use check boxes, and it works on my sites. Also, note that where you have value='Agree' in your input, that doesn't get used. If you want to check an input box you write... checked="yes"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.