Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery not keeping track of validation errors correctly
    primarykey
    data
    text
    <p>I have a small jQuery/AJAX script that checks if a username is already in use or not. It queries a PHP script, which in turn outputs the number of users with the specified username. I have an error counter that keeps track of the number of validation errors on the form, and I increment the counter if the username is already in use.</p> <pre><code>//Keep track of number of errors var errors = 0; //Keep track of error messages var errorMsg = ""; //Check if the passwords match (this part works as expected) if($("#password").val() != $("#repeat").val()) { errors++; errorMsg += "&lt;li&gt;Passwords do not match!&lt;/li&gt;"; } //Now check if the user exists var userName = $("#user").val(); $.ajax({ type: "POST", data : {'user': userName}, cache: false, url: "js/ajax/addUser.php?action=checkuser", success: function(data){ var result = parseInt(data); //For debugging purposes alert("Before checking the data, # of errors is: " + errors); if(result &gt; 0) { errors++; errorMsg += "&lt;li&gt;A user with the name &lt;strong&gt;" + userName + "&lt;/strong&gt; already exists!&lt;/li&gt;"; } //For debugging purposes alert("After checking the data, # of errors is: " + errors); } }); //For debugging purposes alert("Before validating the form, # of errors is: " + errors); if(errors &gt; 0) { //Output error message } else { //Send the form } </code></pre> <p>As mentioned in the comments, I have a few <code>alert()</code>'s for debugging. The first two (within the AJAX request) display the correct amount of errors. However, when I get to the third one, it completely disregards any errors that occurred within the AJAX request.</p> <p>I have trimmed my code down to just the essentials, but if the error is not clear within the snippet provided, I can post the entire thing.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload