Jquery Validate Remote is failing. Can't submit form
I have what used to be a working form validation, including the remote check of a username as available. we've added a lot of other javascript to the script.js file and at some point recently the remote part of this broke. There is only one form field being checked by validate, the new_name field. It is required (works) and must be available (doesn't work).
Here is the jQuery:
$('#nickname_form').validate({
rules: {
new_name: {
required: true,
remote: {
url: '/api/screenname_unique/',
type: 'post'
}
}
},
messages: {
new_name: {
required: 'Please choose a Forum Username.',
remote: 'That Username is already taken or contains invalid characters.'
}
}
});
As I said the above used to work, with no changes. I've checked the rest of the script.js file and no errors are showing up. Also, in the site, we're not seeing JS errors anywhere. If I remove the remote portion of the above code, the required check works and the form will submit when there is a value in the field.
With remote in place, the form will not submit, and whether the ajax response from the remote call is true or false, there is no jQuery validate error message shown. Here is the page called by remote. It works fine as far as the response it is giving for a given value:
<?php
header('Content-type: application/json');
//get the post value
$screen_name = $_POST['new_name'];
//get their member_id
$member_id = $this->EE->session->userdata['member_id'];
//return false if no screen_name provided
if((!$screen_name) || (!$member_id)) {
echo json_encode(false);
exit;
} else { //there is a screen_name
//Regex Check for valid chars
$valid = !preg_match('/[^a-z0-9_ -]/i',trim($screen_name));
if (!$valid) {
echo json_encode(false);
exit;
}
//SQL
$results = $this->EE->db->query("SELECT member_id FROM exp_members WHERE screen_name = '$screen_name' and member_id <> '$member_id' limit 1");
if ($results->num_rows() > 0) {
echo json_encode(false);
} else {
echo json_encode(true);
}开发者_Go百科
}
?>
I am not really sure where to go from here. Ideas?
I was using JQ 1.5.1 and just updated to 1.6.3 and now the form is working fine. I guess maybe there was an issue with remote and 1.5.1? Thanks guys.
You does not sent the data with remote option.
Try this one. It can be helpful to you.
$("#nickname_form").validate({
rules: {
Name: "required",
email: {
required: true,
email: true
},
username:
{
required: true,
remote: {
url: "http://www.xyz.com/checkusername.php",
type: "post",
data: {
username: function() {
return $("#username").val();
}
}
}
}
},
messages: {
Name: "Please enter name.",
email: {
required: "Email address require.",
email: "Please enter valid email address."
},
username:
{
required: " Please enter username.",
remote: " Username is already exists please choose other."
}
},
errorPlacement: function(error, element) {
error.appendTo(element.next());
},
submitHandler: function() {
sendmail();
},
success: function(label) {
label.html(" ").addClass("valid_small");
}
});
And on the server side don't use json in remote option. Print true or false.
<?php
{
$screen_name = $_POST['username'];
$member_id = $this->EE->session->userdata['member_id'];
$results = $this->EE->db->query("SELECT member_id FROM exp_members WHERE screen_name = '$screen_name' and member_id <> '$member_id' limit 1");
if(count($dataArray)>0)
{
echo "false";die;
}
else
{
echo "true";die;
}
}
?>
精彩评论