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. 

**EDIT**
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.

**Edit**
Here is my attempt but it doesn't seem to be working.

**tempregister1.php**

```
<?php

$fname=$_POST['fname];

echo' <!DOCTYPE html>  
  <html lang="en"> 
  <head> 
  <title></title>
  <meta charset="utf-8">

  <script>
    $(#submitbtn).click(function() {

     var fname = '.$fname.';     
      $.ajax({
      type: "POST",
      url: "tempregister2.php",
      data: {fname:fname}
     })    
   });
  </script>
 </head>
 <body>
 <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
 <input type="image" name="submit" id="submitbtn" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" >
 </form>
 </body>
 </html> 
 '; //end php echo
 ?>

```

**tempregister2.php**

```
<?php

$fname=$_POST['fname']; 

require_once('db.php');
$conn = db_connect();

$result = $conn->prepare("INSERT into temp (FirstName) VALUES(?)");
$result -> bind_param('s',$fname);
$result->execute();

?>

```

**EDIT** 
Problem Solved:
I needed to add this before the script in tempregister1.php

```
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $(#submitbtn).submit(function() {

```

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'].';  

Thanks to everyone who helped