Forum Moderators: coopster

Message Too Old, No Replies

Submitting an HTML Form from PHP

         

SeanF

2:39 pm on May 10, 2021 (gmt 0)

5+ Year Member Top Contributors Of The Month



I am trying to write a routine which will loop through a number of sales records and process a credit card payment for each record. The script uses a form which I use for submitting a single payment and I know if works.

The basic intent is this:
- Loop through all current sales records where amount due is > 0
- If the customer has a credit card on file, submit sales_id and payment amount to the credit card form

So, I am using the following code inside of the loop:

  $chargeURL = 'https://www.domainName.com/process_saved_card.php';
$form = "<form id='chargeForm' action='". $chargeURL."' method='post'>";
$form .='<input type="hidden" name="sales_id" value="'. $sales_id.'">';
$form .='<input type="hidden" name="amount_to_charge" value="'. $amount_to_charge.'">';
$form .='<input type="hidden" name="ref" value="'. $ref.'">';
$form .='<input type="hidden" name="session" value="'. $currentSessionID.'">';
$form.="<script type=\"text/javascript\"> document.forms['chargeForm'].submit();</script>";
echo $form;


This is successful in submitting and processing the payment for the first record but then process_saved_card.php terminates and the loop ends.

How do I get the code to continue iterating through the remaining sales items?

Thanks

lucy24

3:34 pm on May 10, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am using the following code inside of the loop
Let's see the loop itself too, in case that's the problem. Is it a “while” construction, a “foreach” construction, or something else?

SeanF

5:18 pm on May 10, 2021 (gmt 0)

5+ Year Member Top Contributors Of The Month



OK, great... Thanks for the quick reply

The Entire scripts is a two part page.

The first part is a <form> which lists all of the companies, includes a checkbox whether or not to charge that company and includes a text field so that the user can modify the amount to be charged. This creates arrays for the "Company ID", "Sales ID", "Charge Y/N", "Amount to Charge"

Then, when that form is submitted, the page executes a "foreach" loop like this:
foreach ($co_id AS $company_id){
$charge_co = $charge[$i];
$amount_to_charge = $amount2charge[$i];
$sales_id = $sls_id[$I];
// echo statements are only for debugging
echo "Co. ID: $company_id<br/> ";
echo "SLS. ID: $sales_id<br/> ";
echo "Amt: $amount_to_charge<br/> ";
$chargeURL = 'https://www.domainName.com/process_saved_card.php';
$form = "<form id='chargeForm' action='". $chargeURL."' method='post'>";
$form .='<input type="hidden" name="sales_id" value="'. $sales_id.'">';
$form .='<input type="hidden" name="amount_to_charge" value="'. $amount_to_charge.'">';
$form .='<input type="hidden" name="ref" value="'. $ref.'">';
$form .='<input type="hidden" name="session" value="'. $currentSessionID.'">';
$form.="<script type=\"text/javascript\"> document.forms['chargeForm'].submit();</script>";
echo $form;
}

If I comment out the " echo $form;" line, the loop successfully iterates through all company records

Also, the "process_saved_card.php" script has a header() statement at the end which normally takes the user to a "Transaction result Page" (for a single transaction) but I have conditionally excluded it for this purpose. If I remove the "if" condition, the header() statement works...

How do I get the routine to return to the calling loop?

Thanks again

lammert

6:14 pm on May 10, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Once the submit is started, the browser leaves the current page and loads the process_saved_card.php script. It will never return to this page. You have to tell the browser that it needs to process the payment in another browser tab and leave this one open. One way to achieve that is by changing the form tag to:
$form = "<form id='chargeForm' target='_blank' action='". $chargeURL."' method='post'>";

SeanF

1:27 am on May 12, 2021 (gmt 0)

5+ Year Member Top Contributors Of The Month



OK, Thanks for the reply.

That makes sense but won't that leave the browser tab open and the user will wind up with dozens (or more) browser tabs?

is there a command to close the tab when the charge is complete? Or better yet, can the tab open "in the background"?

(I really need to figure out how to call the gateway script directly but I haven't done that yet)

lammert

6:52 am on May 12, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, that will create many tabs and generates a lousy user experience. But having people pay dozens of times an amount of money is a lousy user experience anyway.

Another option is a complete rewrite of the user interface where the payment script is called through multiple AJAX calls. This is only possible though if you are sure that no user interaction is necessary during the payment processing. With this solution you could even put a progress indicator in the screen.

The last option is to create an iframe on the page and redirect the payment processing to that iframe. The rest of the page doesn't change, but the user can see the payment progress and when necessary interact with the processing.

SeanF

11:37 am on May 12, 2021 (gmt 0)

5+ Year Member Top Contributors Of The Month



All good points. Thank you.

WRT the user experience, the "user" is an administrator charging the accounts of multiple customers, one payment each. So no-one is paying "dozens of times an amount of money". The current system allows the administrator to charge customer accounts, one at a time. The point of this is to "batch" all customer accounts in a single step.

Thanks again.