title: How to run PHP in the background using JavaScript
body: <p>I'm trying to submit a php form to a database, but I want that when I click the submit button the page refreshes itself (without having to go to the php page I'm submitting to). I understand I have to do this using JS, and I have tried a bunch of different ways without any success. I'm using three files, one for the form, one for the JS, and the last one to post the data into the database. I'm new to php and JS, any help will be very welcome, thanks.</p>
  
  <p>PD here is the code I have so far:</p>
  
  <p><strong>user.php</strong></p>
  
  <pre><code>  &lt;head&gt;
      ........
  
      &lt;!--                       Javascripts                       --&gt;
  
      &lt;!-- jQuery --&gt;
      &lt;script type="text/javascript" src="resources/scripts/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
  
  
  
      &lt;script type="text/javascript" src="resources/scripts/newuser.form_jquery.js"&gt;&lt;/script&gt;
  
      &lt;/head&gt;
  
      &lt;body&gt;
        ...................................
            ......................................
                     ......................................
  
              &lt;form action="" id="newuserform" method="post"&gt;
                  &lt;h4&gt;New user&lt;/h4&gt;
                  &lt;br /&gt;&lt;br /&gt;
  
                  &lt;fieldset class="column-left"&gt;                  
                      &lt;p&gt;
                          &lt;label&gt;Email&lt;/label&gt;
                          &lt;input class="text-input medium-input" type="text" id="email" name="email" /&gt;
  
                          &lt;br /&gt;
                      &lt;/p&gt;
  
                      &lt;p&gt;
                          &lt;label&gt;First name&lt;/label&gt;
                          &lt;input class="text-input medium-input" type="text" id="fname" name="fname" /&gt;
  
                          &lt;br /&gt;
                      &lt;/p&gt;                    
                  &lt;/fieldset&gt;
  
                  &lt;fieldset class="column-right"&gt;
                      &lt;p&gt;
                          &lt;label&gt;Password&lt;/label&gt;
                          &lt;input class="text-input medium-input" type="password" id="pwd" name="pwd" /&gt;
  
                          &lt;br /&gt;
                      &lt;/p&gt;
  
                      &lt;p&gt;
                          &lt;label&gt;Last Name&lt;/label&gt;
                          &lt;input class="text-input medium-input" type="text" id="lname" name="lname" /&gt;
  
                          &lt;br /&gt;
                      &lt;/p&gt;
                  &lt;/fieldset&gt;             
                      &lt;p&gt;
                          &lt;input class="button" type="button" value="Create" id = "submit_btn"/&gt;
                      &lt;/p&gt;
                  &lt;div id="errormsg" class="notification error png_bg"&gt;
                      &lt;div&gt;
                          Invalid login, please check your username and password!
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
                   &lt;/body&gt;
  </code></pre>
  
  <p><strong>newuser.form_jquery.js</strong></p>
  
  <pre><code>   $(document).ready(function() {
      $("#errormsg").hide();
       $("#newuserform").keypress(function(e){
      if(e.which == 13){
          $("#errormsg").hide();
          jQuery('#submit_btn').focus().click();
          e.preventDefault();
          return false;
      }
  });
  
  
  $("#submit_butn").click( function() {
  
  
      var e = '#email';
      var p = '#pwd';
      var f = '#fname';
      var l = '#lname';
  
      var _e = $(e).val();
      var _p = $(p).val();
      var _f = $(f).val();
      var _l = $(l).val();
  
  
      var postFile = 'application/post.newuser.php';
      var dataurl = 'users';
  
      $.post(postFile, { e: _e, p: _p, f: _f, l: _l}, function(data) {
          if(data.status==true){
              window.location=dataurl;
          } else {
  
              $("#errormsg").show();
  
          }
      }
  
      ,'json');
  
  });
  
      });
  </code></pre>
  
  <p><strong>post.newuser.php*</strong></p>
  
  <pre><code>    public function newuser($e, $p, $f, $l)
  {
      //e: email, p: password, f: first_name, l: last_name
  
      $return = false;
  
      $sql = "INSERT INTO users (email, password, first_name, last_name, role, enabled)";
      $sql .= " VALUES (";
      $sql .= "'".$e."', ";
      $sql .= "'".SHA1($p)."', ";
      $sql .= "'".$f."', ";
      $sql .= "'".$l."', ";
      $sql .= "'2', ";
      $sql .= "'1' ";
      $sql .= ")";
  
      $mysql = new mysql();
      $mysql-&gt;connect();
      $q = $mysql-&gt;queryin($sql);
  
      if ($q)
      {
          $return = true;
      } 
  
      $mysql-&gt;end(); 
  
      return $return;
  
  }
  </code></pre>
  
  <p>Sorry for the long post.</p>