eoin
eoin

Reputation: 113

how to submit data to my database when the paypal buynow button is clicked from my cart

hi there Iam creating a Paypal IPN and i have a pay now button in my cart page which is of type image. Im used to using type submit. i was just wondering how i would write to my database on the submition of this pay now button. what i mean is i want to write to the database as this button is clicked but im not sure exactly how to do it? here is my code for sending my data to paypal.

<?php


 if(isset($_SESSION['username']) && isset($_SESSION['itemnames']))
   {
   $checkoutbutton .='<form action="https://www.sandbox.paypal.com/cgibin/webscr"     method="post">
     <input type="hidden" name="cmd" value="_cart">
     <input type="hidden" name="business" value="xxxxxx">
     <input type="hidden" name="upload" value="1">';

  ?> <ul2>

   <?php


  for ($i = 0;$i <count($_SESSION['itemnames']);$i++)
     { ?>
          <li>

         <?php
         $x = $i + 1;
        echo "<h3>";
        echo $_SESSION['itemnames'][$i];   
        echo "</h3>";
        echo "<b>Cost per Item:  &euro;" .$_SESSION['price'][$i] ."</br> Number of Items: " . $_SESSION['quantity'][$i] ."</b>";


        $checkoutbutton .='<input type="hidden" name="item_name_'.$x.'" value="'. $_SESSION['itemnames'][$i] .'">  
        <input type="hidden" name="amount_'.$x.'" value="' .$_SESSION['price'][$i] . '">
        <input type="hidden" name="quantity_'.$x.'" value="' .$_SESSION['quantity'][$i] . '">';
          ?>

         </li>
         <?php }      $checkoutbutton .='<input type="hidden" name="notify_url" value="http://ipnscript.php">
                                         <input type="hidden" name="return" value="http://ranscomplete.php">
                                         <input type="hidden" name="rm" value="2">
                                         <input type="hidden" name="cbt" value="return to store">
                                         <input type="hidden" name="cancel_return" value="http://www.theislandapp.com/transcancelled.php">
                                         <input type="hidden" name="currency_code" value="EUR">
                       <input type="image" src="x-click-but5.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">';

          ?>   

          </ul2> 


               <?php }

              else {?>
              <h2> No Items in cart </h2>
            <?php } ?> </br></br>

Upvotes: 0

Views: 1166

Answers (2)

LmOtaku
LmOtaku

Reputation: 31

You could always use ajax(jquery) to call a page in the background, it doesn't even have to display any data.

$.post("save.php", { name: "John", item: "143" } );

http://api.jquery.com/jQuery.post/

By giving your buy button an id, and using click function would make this possible.

<input id="yebeclicked" type="image" src="x-click-but5.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
<script type="text/javascript">
$("#yebeclicked").click(function() {
 $.post("save.php", { name: "John", item: "143" } );
});
</script>

Cheers. ;)

Upvotes: 0

jshrc
jshrc

Reputation: 197

You'll need to add a step before heading to PayPal. Basically your cart would POST to another script on your site, which would log whatever info you need, and then you can loop through your cart and send things over to PayPal via a GET string on the URL. I believe the variables are the same, plus you can even get their name and address info to store on your side beforehand.

Upvotes: 1

Related Questions