Reputation: 5367
I'm running a form through an AJAX post call, but the value on my checkbox remains the same, regardless if I check or uncheck it when the mail script is processed.
I want the value to change when unchecked. So unchecking the checkbox changes the value to "no", etc.
Here's my code, what am I doing wrong?
<input type="checkbox" name="newsletter" id="newsletter" checked="checked" value="1"/>
Ajax Call is here:
$(document).ready(function(){
$("#submitButton").click(function() {
// PROCESS FORM
var name = $("#name").val();
var email = $("#email").val();
var phone = $("#phone").val();
var postal = $("#postal").val();
var newsletter = $("#newsletter").val();
var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&postal=' + postal+ '&newsletter=' + newsletter;
if (name=='' || name=='First Name' ){
$('#name').removeClass("reg-bg");
$('#name').addClass("error-bg");
}
if (email=='' || email=='Email Address'){
$('#email').removeClass("reg-bg");
$('#email').addClass("error-bg");
}
if (postal=='' || postal=='Postal Code'){
$('#postal').removeClass("reg-bg");
$('#postal').addClass("error-bg");
return false;
}
else {
$.ajax({
type: "POST",
url: "mailer.php",
data: dataString,
success: function(){
$(".formPanel").slideUp(200);
$(".formPanel2").delay(600).slideDown(200);
}
});
}
return false;
});
});
Php Mail Script here:
<?php
$to ="[email protected]";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$postal = $_POST['postal'];
$newsletter = $_POST['newsletter'];
$subject = "FREE TRIAL INQUIRY";
$headers = "From: ".$email;
$body = "This person has requested a FREE TRIAL at the studio nearest them...
Name: $name
Email: $email
Phone: $phone
Postal: $postal
Newsletter: $newsletter";
mail($to, $subject, $body, $headers);
?>
Upvotes: 1
Views: 499
Reputation: 1405
You have to check the value of the checkbox in your PHP Mail script, and handle it slightly differently.
My approach would be to do this:
if(isset($_POST['newsletter'])&&$_POST['newsletter']==1) $newsletter="yes"; else $newsletter="no";
You are using the PHP to determine the value in this case, since a checkbox can only have one value! If it's checked, the value will be that of the html value, but in my example we are only using the value of the HTML field in our if statement:
if (isset($_POST['newsletter']))
//checks to see if this element exists on form submit, and
$_POST['newsletter']==1
//checks to see if the checkbox is checked. If you echo back the value of $_POST['newsletter']
it will always be one, so you need to adjust the logic of your mail script so that you are using the style of "if checked, then yes; else no" type of thing.
Upvotes: 1
Reputation: 12059
The value of the checkbox will always be 1 regardless of whether or not it is checked when viewing the value of it using jQuery.
What you need to do is see if the checkbox is checked:
var newsletter = $("#newsletter").is(":checked")?1:0;
Upvotes: 1
Reputation: 9200
$('#newsletter').click(function(){
if($(this).val() == 1) {
$(this).val('no');
// I'd recommend doing it this way for consistency sake
$(this).val(0);
}
});
Upvotes: 1