最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 pletes, 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!

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 pletes, 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!

Share Improve this question edited Dec 31, 2013 at 16:17 user3122466 asked Dec 31, 2013 at 16:10 user3122466user3122466 431 gold badge1 silver badge7 bronze badges 5
  • You should look into AJAX. – Marcel Korpel Commented Dec 31, 2013 at 16:15
  • PHP is side server, it will executed when your data Form is submitted to the server, you need to use Ajax to call your remote PHP function. – hrnnvcnt Commented Dec 31, 2013 at 16:43
  • I am not very familiar with AJAX, but I will look into using it, if I get some guidance but I have read some ments that suggests that using AJAX might fail if the user has JS disabled. Is this true? – user3122466 Commented Dec 31, 2013 at 16:50
  • Do you really need to fully support no-js users? If so your "onsubmit" tag won't work either in addition to AJAX and you would have to have a standard form post/pletely server-side solution anyway. – codercake Commented Dec 31, 2013 at 18:13
  • Very true! I have no idea what the percentage of no-js users are out there ...probably very small. However, my code will break in a sense I would then end up processing a users payment via paypal like pany without any record of it being registered on my database for that one no-js user. I dunno ...:( – user3122466 Commented Dec 31, 2013 at 19:19
Add a ment  | 

2 Answers 2

Reset to default 2

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 pletely 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 pletely ignore the possibility your staging data insert could fail and let the payment processor deal with it
   }
</script>

See http://api.jquery./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 pletely separate table.

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

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论