简体   繁体   中英

How do I execute php function on submit button in a FORM similar to onsubmit=“ return javascript function”?

My goal: To process three objectives: (1) Make insertion in a staging table before processing payment. (2) Go to third party payment processing site like paypal using my form action attribute. (3) Read the $_POST data from the payment landing page to determine if payment was successful and then take the data from the staging table and insert into real table.

My desired implementation would sth like:

 <form name="demo" method="post" onsubmit="<?php insertStagingData() ?>" action="3rdpartyPaymentProcessingLink"> <input name ="Submit" type"submit" value"Submit"> </form> 

I know that the web browser executes the onsubmit handler and when the handler completes, the browser proceeds with the form submit. I have used this concept for java script client side validation before forwarding the form to web server for php processing.

I dont know if this can be done when both handlers require server side processing, one on my webserver and the second on a 3rd party site.

Thanks in advance for suggestions and help!

PHP is a server-side scripting language. which means it executes code when a page request is made. Thing like onclick browser events can not cause a webserver to run code.

To facilitate this in PHP you will need to initiate the PHP function on the target form page.

Look into Paypal APIs in order to have a more advanced implementation.

The theory of a no-js solution would be something like this:

<form name="demo" method="post" action="/scriptOnYourServer.php"> 
   <input name ="Submit" type"submit" value"Submit"> 
</form>

And then completely within scriptOnYourServer.php you would do any validation you want first, then do the staging table insert, then use the 3rd party payment PHP API (or restful service or soap service - whatever is available) to process payment - which you should then get a success/fail from, and you either display errors/offer retry or move from staging to a real table - all within this one request to your script.

If your goal is to avoid writing payment API integration code in PHP, then your basic outline will only work with javascript/AJAX if you must perform server side actions and still have the form action go to the payment processor, and typically a payment vendor that offers a form post solution will allow a "return URL" they can send the success/fail to where you do further processing which I think you are describing in your step 3.

So that is more like:

    <form name="demo" method="post" onsubmit="jsFunctionName()" action="3rdpartyPaymentProcessingLink"> 
       <input name ="Submit" type"submit" value"Submit"> 
    </form>
<script type="text/javascript">
   function jsFunctionName() {
      // js ajax code, probably easiest to use a lib like jQuery for this
      // You tell it the url on your server you will post vars to / generate a success/fail response, probably in json is best.
      // based on your servers "insert into staging table" success/fail json response you decide whether to proceed or not (allow the submit to 3rd party), because you would not want to proceed if your staging table insert failed I presume
      // so this implies you have a way to display errors in JS on this page, or you will be redirecting to a script of yours which would reload the page but display any errors - or you just completely ignore the possibility your staging data insert could fail and let the payment processor deal with it
   }
</script>

See http://api.jquery.com/jQuery.ajax/

So with this a few moving parts - your script - insertStagingData.php - it's job is to read the post data, you should do validation of some sort, then insert into your staging database (make sure you are using parametrized queries or sanitizing data since you are dumping user data into a database), then it generates a json response like:

{"success":true}

or

{"success":false}

And you need to have PHP use a JSON header - so at the end of this script you need to do something like this:

$response = array("success" => true); // or false depending on if your db insert was successful
header("HTTP/1.1 200 OK");
header('Content-type: application/json');
echo json_encode($response);

Then after that in the script block above in jsFunctionName, you will read the "success" variable from your json response and either display an error and return false or use preventDefault() to stop the submit to the 3rd party script, or if it is a success you allow the form submit to post.

Then you can setup a separate script for the payment processor to post back to where their docs will tell you what data they will post to you, and based on their success/fail you can display an error, or success and then move the data to the real table.

You may also want to consider the staging/real table should be the SAME table, with a column like payment_success = 0 or 1 where it is 0 by default and if the payment processor posts back success, you just update the record to payment_success=1. There are very few good reasons this should be a completely separate table.

This should hopefully help point you in the right direction. Good luck

what you can do is post the data inside of the same variable $_POST["TODO"] to with javascript/ajax to a second page. on the second page, you parse the data, then submit it. it is possible to post an array with two get variables inside of it, each containing its own array of form info. hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM