title: Ajax PHP + MySQL registration form tutorial
body: <p>Until now I have
  HTML</p>
  
  <pre><code>&lt;form id="account_reg" action="reg.php" method="post"&gt;
      &lt;div id="response"&gt;&lt;/div&gt;
      &lt;div class="input"&gt;
        &lt;label&gt;Login&lt;/&gt;
        &lt;input name="login" type="text" class="required" id="login" size="30"/&gt;
      &lt;/div&gt;
  
      &lt;div class="input"&gt;
      &lt;label&gt;Password&lt;/&gt;
      &lt;input name="password" type="text" class="required" id="password" size="30"/&gt;
      &lt;/div&gt;
  
      &lt;div class="input"&gt;
      &lt;label&gt;Repeat Password&lt;/&gt;
      &lt;input name="rpassword" type="text" class="required" id="rpassword" size="30"/&gt;
      &lt;/div&gt;
  
      &lt;div class="input"&gt;
      &lt;label&gt;Email&lt;/&gt;
      &lt;input name="email" type="text" class="required" id="email" size="30"/&gt;
      &lt;/div&gt;
  
      &lt;div class="button"&gt;
      &lt;input type="submit" name="send" id="send" value="Send"/&gt;
      &lt;/div&gt;
  
      &lt;div class="input"&gt;
      &lt;input type="hidden" name="honeypot" id="honeypot" value="http://"/&gt;
      &lt;input type="hidden" name="humancheck" id="humancheck" class="clear" value=""/&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>MY PHP
  I have some validation on server side.
  `</p>
  
  <pre><code>include("dop/config.php"); //includ Db connect
  
      $login = ($_POST['login']);
      $password = ($_POST['password']);
      $rpassword = ($_POST['rpassword']);
      $email = ($_POST['email']);
      $humancheck = $_POST['humancheck'];
      $honeypot = $_POST['honeypot']; 
  if ($honeypot == 'http://' &amp;&amp; empty($humancheck)) { 
  $error_message = '';
  $reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-Za-Z.](2,4)$/";
  if(!preg_match($reg_exp, $email)){
  $error_message .="&lt;p&gt;A Valid email is required&lt;/p&gt;";
  }
  if(empty($login)){
  $error_message .="&lt;p&gt;enter login&lt;/p&gt;";
  }
  if(empty($password)){
  $error_message .="&lt;p&gt;Enter password&lt;/p&gt;";
  }
  if(empty($rpassword)){
  $error_message .="&lt;p&gt;Enter password again&lt;/p&gt;";
  }
  if($password != $rpassword){
  $error_message .="&lt;p&gt;password mut match&lt;/p&gt;";
  }
  } 
  else {
  $return['error'] = true;
  $return['msg'] = "&lt;h3&gt;There was a problem with your submission. Please try again.&lt;/h3&gt;";    
      echo json_encode($return);
  } 
  </code></pre>
  
  <p>My JS
  With Validation.</p>
  
  <pre><code>$('#send').click(function(e)
      {
          e.preventDefault();
          var valid = '';
          var required =' is required.';
          var login = $('#account_reg #login').val();
          var password = $('#account_reg #password').val();
          var rpassword = $('#account_reg #rpassword').val();
          var email = $('#account_reg #email').val();
          var honeypot = $('#account_reg #honeypot').val();
          var humancheck = $('#account_reg #humancheck').val();
  
          if(login = ''){
              valid ='&lt;p&gt; Your Name '+ required +'&lt;/p&gt;';
          }
          if(password='' || company.length&lt;=6){
              valid +='&lt;p&gt; Password '+ required +'&lt;/p&gt;';
          }
          if(rpassword != password){
              valid +='&lt;p&gt; password must match &lt;/p&gt;';
          }
          if(!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i))
          {
              valid +='&lt;p&gt; Your Email' + required +'&lt;/p&gt;';
          }
  
  
          if (honeypot != 'http://') {
              valid += '&lt;p&gt;Spambots are not allowed.&lt;/p&gt;';    
          }
  
          if (humancheck != '') {
              valid += '&lt;p&gt;A human user' + required + '&lt;/p&gt;'; 
          }
          if(valid !=''){
              $('#account_reg #response').removeClass().addClass("error")
                  .html('&lt;strong&gt; Please Correct the errors Below &lt;/strong&gt;' + valid).fadeIn('fast');
          }
          else{
              //$('form #response').removeClass().addClass('processing').html('Processing...').fadeIn('fast');
              var formData = $('#account_reg').serialize();
              $('#send').val("Please wait...");
              submitForm(formData);
          }
      });
  function submitForm(formData) {
  
  $.post('reg2.php',formData, function(data){
      //console.log(data);
      $('#send').val("Send");
      if (data === '1') {
          alert('ok!');
      } else {
          alert('wrong shit');
      }
  });
  };
  </code></pre>
  
  <p>I didn't do yet the MySQL part With Inserting and checking if account is already registered.</p>
  
  <p>The first problem that I have is on Click event redirects me on reg.php even when I use <code>e.preventDefault();</code></p>
  
  <p>If anybody knows a good tutorial for creating registration form or any suggestion.</p>