Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery AJAX callback: identify if the return value from the PHP code has a given pattern
    text
    copied!<p><strong>The Objective:</strong><br> There is a login form where the user types in the username and password and clicks on a login button <code>(type="submit")</code>. I am trying to identify if the return value from the PHP code has the pattern <code>"FailText"</code> in it. If yes, display the message in div <code>"signinAlert"</code>, else (if login is successful) redirect to the user's home page.</p> <p><strong>The Code:</strong> </p> <pre><code>&lt;form id="loginForm" name="loginForm" method="POST"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;input class="input_home" type="text" name="username" id="email" size="30" placeholder="E-mail Address" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input class="input_home" type="password" name="password" id="password" size="30" maxlength="12" placeholder="Password" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="text-align: right;"&gt;&lt;a href="#" title="Forgot Password"&gt;Forgot Password?&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;input type="submit" class="Button" value="Sign In" id="login" onclick="return DisplayLoginError()"; /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p>Here is my JavaScript function. </p> <pre><code>function DisplayLoginError() { var loginForm = document.loginForm; var dataString = $(loginForm).serialize(); $.ajax ({ type: 'POST', url: 'login.php', data: dataString, success: function(data){ var result = data; var patt1=new RegExp("FailText"); if (patt1.test(result)) { $('#signinAlert').html(data); $('#signinAlert').css('display','block'); } else { window.location = "home/"; } } }); return false; } </code></pre> <p><strong>The question:</strong> I strongly believe that the above JavaScript code is very sloppy (because of the RegExp evaluation when the login attempt is a success). Is there any other way to achieve the objective? By the way, the above method works on ALL major browsers. </p>
 

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