title: Inserting values into database with php, ajax and jQuery
body: <p>I'm struggling very hard to get this to work and I don't know what I'm doing wrong. I have a register page that I want to take the data inserted into the form and INSERT it to the database with jQuery and AJAX. I'm not very experienced with AJAX AND jQuery so be gentle! :P I will show you the files that I have... </p>
  
  <p><strong>sign_up.php</strong></p>
  
  <pre><code>    &lt;?php
      include 'connect.php';
      include 'header.php';
  
      echo '&lt;h2&gt;Register&lt;/h2&gt;';
  
      if($_SERVER['REQUEST_METHOD'] != 'POST')
      {
          echo '&lt;br/&gt;';
          echo '
          &lt;div class="container"&gt;  
          &lt;form id="submit" method="post" action=""&gt;  
          &lt;fieldset&gt;  
          &lt;legend&gt; Enter Information &lt;/legend&gt;  
          &lt;br/&gt;
          &lt;label for="user_name"&gt;Your username: &lt;/label&gt;
          &lt;br/&gt;
          &lt;input id="user_name" class="text" name="user_name" size="20" type="text"&gt;
          &lt;br/&gt;
          &lt;br/&gt;
          &lt;label for="user_pass"&gt;Your password: &lt;/label&gt;
          &lt;br/&gt;
          &lt;input id="user_pass" class="text" name="user_pass" size="20" type="password"&gt;  
          &lt;br/&gt;
          &lt;br/&gt;
          &lt;label for="user_pass_check"&gt;Confirm password: &lt;/label&gt;
          &lt;br/&gt;
          &lt;input id="user_pass_check" class="text" name="user_pass_check" size="20" type="password"&gt;
          &lt;br/&gt;
          &lt;br/&gt;
          &lt;label for="user_email"&gt;Email: &lt;/label&gt;
          &lt;br/&gt;
          &lt;input id="user_email" class="text" name="user_email" size="20" type="email"&gt;
          &lt;br/&gt;
          &lt;br/&gt;
          &lt;button class="button positive" type="submit"&gt; &lt;img src="like.png" alt=""&gt; Register &lt;/button&gt;  
          &lt;/fieldset&gt;  
          &lt;/form&gt;  
          &lt;div class="success" style="display: none;"&gt; You are now a registered user!&lt;/div&gt;  
          &lt;/div&gt;';
      }
      else {
          $errors = array();
          //Checks the errors
          if(isset($_POST['user_name']) == NULL)
          {
                  //if the user name is larger than 30 characters
                  $errors[] = 'Please enter your username.';
          }   
  
          //Checks the password
          if(isset($_POST['user_pass']) == NULL)
          {
                  $errors[] = 'Please enter your password.';
          }
          else
          {
                  if($_POST['user_pass'] != $_POST['user_pass_check'])
                  {
                      $errors[] = 'The two passwords did not match.';
                  }
          }
  
          if(!empty($errors)) //Checks for empty fields
          {
                  echo 'Please check that all the fields are filled in.&lt;br /&gt;&lt;br /&gt;';
                  echo '&lt;ul&gt;';
                  foreach($errors as $key =&gt; $value) //walk through the array so all the errors get displayed
                  {
                          echo '&lt;li&gt;' . $value . '&lt;/li&gt;'; //this generates a list with errors
                  }
                  echo '&lt;/ul&gt;';
          }
   }
  ?&gt;
  </code></pre>
  
  <p>in my header.php (which I include in every page) I included <strong>addMembers.js</strong></p>
  
  <pre><code>    $(document).ready(function(){  
          $("form#submit").submit(function() {
  
          // we want to store the values from the form input box, then send via ajax below  
          var user_name = $('#user_name').val();  
          var user_email = $('#user_email').val();
          var user_pass= $('#user_pass').val();
  
          //$user_name = $('#user_name').val();
          //$user_email = $('#user_email').val();
          //$password = $('#password').val();
  
          alert(user_name);
  
              $.ajax({  
                  type: "POST",  
                  url: "ajax.php",  
                  data: "user_name="+ user_name +"&amp;user_email="+ user_email +"$user_pass=" + user_pass,  
                  success: function(){  
                      $('form#submit').hide(function(){$('div.success').fadeIn();});  
                  }  
              });
  
              //alert("ji");
  
          return false;  
          });  
  });  
  </code></pre>
  
  <p>and then my ajax.php that gets the data and must insert it into the database but it doesn't! :(</p>
  
  <pre><code>&lt;?php
          include 'connect.php';
          include 'header.php';
  
          // CLIENT INFORMATION  
          $user_name  = $_POST['user_name'];  
          $user_email   = $_POST['user_email'];  
          $user_pass   = $_POST['user_pass'];  
  
          mysql_query("INSERT INTO
                        users(`user_name`, `user_pass`, `user_email` , `user_date` , `user_level`)
                  VALUES('$user_name', '$user_pass', '$user_email', NOW() , 0)" OR trigger_error(mysql_error())); 
  ?&gt;
  </code></pre>
  
  <p>PLEASE help...</p>
  
  <p>Thanks a lot!</p>
  
  <p>Joe</p>