title: Integrate a PhP function with JQuery - Check if User already exists in DB
body: <p>i have a php code to check if a user already exists in my db , but i would like it to go through jquery first .</p>
  <p>This way instead of getting an error message on a new page , the user simply gets a message that the user already exists and can change the name.</p>
  <p>Looks like this :</p>
  <p><a href="https://i.stack.imgur.com/zbODs.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zbODs.jpg" alt="form" /></a></p>
  
  <p>PHP CODE:</p>
  <pre><code>&lt;?php
  
  $link = mysqli_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;&quot;,&quot;data1&quot;) or die(&quot;Error &quot; . mysqli_error($link));
  $usercheck = &quot;csdcsdsdf&quot;;
  
  $sanitizeduser= filter_var($usercheck, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
  
  $result = $link-&gt;query(&quot;SELECT username FROM users WHERE username = '&quot;.$sanitizeduser.&quot;' &quot;);
  
  $row_cnt = $result-&gt;num_rows;    
  if($row_cnt&gt;0){     
      echo &quot;User Exists&quot;;
  }else{      
      echo &quot;No User found&quot;;
  }
  
  ?&gt;
  </code></pre>
  <p>I'm using a predefined jquery code (this script : <a href="http://jqueryvalidation.org/" rel="nofollow noreferrer">http://jqueryvalidation.org/</a>)'</p>
  <p>part of my jquery code to validate names :</p>
  <pre><code>names: function(value, element) {
              return this.optional(element) || /^\w+$/.test(value);
          },
  </code></pre>
  <p>Full Jquery code :</p>
  <p>file 1 : <a href="http://jsfiddle.net/EjSbd/1/" rel="nofollow noreferrer">http://jsfiddle.net/EjSbd/1/</a> (script.js)</p>
  <p>file 2 : <a href="http://jsfiddle.net/qM4Uz/1/" rel="nofollow noreferrer">http://jsfiddle.net/qM4Uz/1/</a> (jquery.validate.js)</p>