title: Call PHP functions from html button click
body: <p>I created a php registration page that saves the user's data from a previous page to my database when the page first loads and also displays a button that directs the user to pay on an external site when they click it. I would like to edit the page to only save the data to the database when the user clicks the pay button. Can I have this button click event call a PHP function or PHP page to handle this processing and also have the user continue with payment on the external site? I'm using a paypal buy now button which is basically a form with hidden input values.  I'm guessing I have to use Ajax but I dont know how. </p>
  
  <p><strong>EDIT</strong>
  Just to clarify what im trying to do. I want to temporarily save registration data to my database before they pay and once they pay that data will be transferred to another table so I know its finalized. I want to save the temp registration data when they click the pay button. The same page that has this pay button also saves the temp  registration data to the database on load. I want to change that so the data isnt saved until the user at least click to pay. otherwise if for some reason the user stops here I end up with a database full of incomplete registrations.</p>
  
  <p><strong>Edit</strong>
  Here is my attempt but it doesn't seem to be working.</p>
  
  <p><strong>tempregister1.php</strong></p>
  
  <pre><code>&lt;?php
  
  $fname=$_POST['fname];
  
  echo' &lt;!DOCTYPE html&gt;  
    &lt;html lang="en"&gt; 
    &lt;head&gt; 
    &lt;title&gt;&lt;/title&gt;
    &lt;meta charset="utf-8"&gt;
  
    &lt;script&gt;
      $(#submitbtn).click(function() {
  
       var fname = '.$fname.';     
        $.ajax({
        type: "POST",
        url: "tempregister2.php",
        data: {fname:fname}
       })    
     });
    &lt;/script&gt;
   &lt;/head&gt;
   &lt;body&gt;
   &lt;form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top"&gt;
   &lt;input type="image" name="submit" id="submitbtn" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" &gt;
   &lt;/form&gt;
   &lt;/body&gt;
   &lt;/html&gt; 
   '; //end php echo
   ?&gt;
  </code></pre>
  
  <p><strong>tempregister2.php</strong></p>
  
  <pre><code>&lt;?php
  
  $fname=$_POST['fname']; 
  
  require_once('db.php');
  $conn = db_connect();
  
  $result = $conn-&gt;prepare("INSERT into temp (FirstName) VALUES(?)");
  $result -&gt; bind_param('s',$fname);
  $result-&gt;execute();
  
  ?&gt;
  </code></pre>
  
  <p><strong>EDIT</strong> 
  Problem Solved:
  I needed to add this before the script in tempregister1.php</p>
  
  <pre><code>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  $(document).ready(function(){
    $(#submitbtn).submit(function() {
  </code></pre>
  
  <p>Not sure if it made a difference but I also change it from a click event to a submit event  and changed var fname = $('.$_POST['fname'].'); to var fname = '.$_POST['fname'].';<br>
  Thanks to everyone who helped</p>