jquery validation plugin remote check on radio button value
I have added a remote validation rule to a set of radio buttons.
I want the server side script to test the value of the radio buttons ('y' or 'n') and return a different value depending on the radio selection.
The problem I am having is that no matter which radio button is selected, the value that is sent to the server is always 'y'
I'm returning only the POST data to make sure
here's the code in question
HTML
<form id="signup-form">
<div>
<label for="mbr_org_name">Org. Name*</label>
<input type="text" name="mbr_org_name" id="mbr_org_name" class="required" />
</div>
<div>
<label for="mbr_tl_account_number">Account Number</label>
<input type="text" name="mbr_tl_account_number" id="mbr_tl_account_number" class="digits" value="" />
</div>
<p class="field-note" id="mbr_credit_note">Have you previously established credit terms with Terri Lynn?</p>
<div class="radio" id="mbr_credit_wrapper">
<label for="mbr_credit_y">Yes, I have.</label> <input type="radio" value="y" id="mbr_credit_y" name="mbr_credit" class="ignore" disabled="disabled" /> <br />
<label for="mbr_credit_n">No, I havn’t.</label> <input type="radio" value="n" id="mbr_credit_n" name="mbr_credit" class="ignore" disabled="disabled" />
</div>
</form>
JS
$('#signup-form').validate({
// enabled for developments
debug: true,
// special validation rules that could not be included as <input /> classes
rules: {
// account numbers are all 6 digits
mbr_tl_account_number: {
minlength: 6,
maxlength: 6
},
// if the account number field has something in it
// then the user must specify if they have credit, y or n
// we will perform a validation against account numbers and org names to make sure this is the case
mbr_credit: {
required: '#mbr_tl_account_number:filled',
remote: {
url: '/tlfr_account_check.php',
//cache: false,
type: 'post',
data: {
mbr_org_name: function() {
return $('#mbr_org_name').val();
},
mbr_tl_account_number: function() {
return $('#mbr_tl_account_number').val();
}
}
}
},
password_confirm: {
equalTo: '#password'
}
},
// special error messages if the fail the credit check
messages: {
mbr_credit: {
required: "Please choose one."
}
}
});
$('#mbr_tl_account_number').focusout(function(evt){
$(this).removeD开发者_Python百科ata("previousValue");
$("#mbr_credit_wrapper input").removeData("previousValue");
if($(this).valid() === 1){
$('#mbr_credit_note').fadeIn('3000');
$('#mbr_credit_wrapper').fadeIn('3000');
$('#mbr_credit_wrapper input').removeClass('ignore').removeAttr('disabled');
}
});
PHP
<?
// make sure this request is coming from ajax
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
$post_data = $_POST['mbr_credit'].'||'.$_POST['mbr_org_name'].'||'.$_POST['mbr_tl_account_number'];
echo '"post '.$post_data.' data"';
}
else{
die();
}
?>
ultimately I gave up on the radio buttons, but I was able to pass the correct checked value by targeting the parent element ... doing something like this
$('#signup-form').validate({
rules: {
mbr_credit: {
remote: {
url: '/tlfr_account_check.php',
data: {
mbr_credit_checked:function() {
return $('div#radio_button_wrapper input:checked').val();
}
}
}
}
});
精彩评论