Jquery Validate Remote does not work. Unable to submit form

I have what used to be a validation of the working form, including remote verification of the username as available. we added many other javascript to the script.js file, and at some point, the recently deleted part of it broke. The check field for only one field is checked only in the new_name field. It is necessary (works) and must be available (does not work).

Here is 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 previous one was used to work unchanged. I checked the rest of the script.js file and no errors were found. In addition, on the site we do not see JS errors anywhere. If I delete the deleted part of the above code, the required validation will be performed and the form will be submitted when the value is in the field.

With the remote in place form will not be submitted and the ajax response from the remote call will be true or false, the jQuery validation error message will not be shown. Here is the page that is being called by the remote. It works fine as far as it is responsible for the 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); } } 

? >

I'm not sure where to go from here. Ideas?

+6
source share
2 answers

I used JQ 1.5.1 and just upgraded to 1.6.3 and now the form is working fine. I think maybe there was a problem with remote and 1.5.1? Thanks guys.

+2
source

You did not send data with the removed option.

Try it. This may be useful for 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("&nbsp;").addClass("valid_small"); } }); 

And on the server side, do not use json in the remote version. 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; } } ?> 
0
source

Source: https://habr.com/ru/post/896760/


All Articles