Full disclosure - I'm not total novice so any help would be appreciated but simple help for dummies will be extra appreciated :)

I'm creating a form in an html page. It needs to link up with a payment gateway and forward the entered information (I'm currently using the method="post" and that's all working fine), but I also need to collect the information and have it sent to a nominated email address (I'm currently using a sendmail.php action).

I have managed to make both work separately, but not together for the obvious reasons.

Is there a way to achieve both functions off the same form submission? And if so what is it?

Dani AI

Generated

Short answer: an HTML form can only have one action. For the situation described (post to a payment gateway and also capture/send a copy by email), the reliable pattern is to post once to a server-side handler that does both: validate/sanitize and email/store the submission, then forward to the payment gateway. That centralised flow avoids race conditions and exposing secrets. 's client-side double-submit trick can work but is brittle (race conditions, popup blockers, duplicate charges). 's point about sanitisation is essential.

Two practical forwarding patterns that fit most gateways:

  • Server-to-server API call: the handler uses the gateway SDK or cURL to create a payment/session with server-held credentials, then redirects the browser to the hosted checkout or returns an approval link. Pros: keys never go to the browser; better control and logging.
  • Browser auto-post: after emailing/logging, the handler returns a tiny HTML page with a hidden form and a short script that auto-submits to the gateway. Pros: simple when the gateway expects a browser POST. Important: do not embed secret keys or raw card data into that HTML; prefer ephemeral tokens.

Minimal PHP skeleton (illustrates flow; replace mail() with PHPMailer or an SMTP/provider API in production):

<?php
// basic sanitise
$name  = filter_input(INPUT_POST,'name',FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL);

// email or log (use PHPMailer/SMTP for reliability)
$to = 'orders@example.com';
$subject = 'New order';
$msg = "Name: $name\nEmail: $email\n";
$headers = 'From: no-reply@example.com' . "\r\n";
mail($to, $subject, $msg, $headers);

// prepare gateway fields and auto-post back to gateway
$post = ['amount'=>'19.99','order_id'=>uniqid()];
echo '<form id="pay" action="https://gateway.example/checkout" method="POST">';
foreach ($post as $k=>$v) {
  echo '<input type="hidden" name="'.htmlspecialchars($k).'" value="'.htmlspecialchars($v).'">';
}
echo '</form><script>document.getElementById("pay").submit();</script>';
exit;
?>

Troubleshooting and security notes: always use HTTPS; test in the gateway sandbox; verify field names match the gateway docs; check server outbound connectivity (cURL) and logs; never store full card data unless PCI-certified; prefer tokenization or server-side session creation; add CSRF checks and strong input validation to prevent injection or tampering.

Recommended Answers

All 3 Replies

i'm not sure but i think important question

I think tats possible...

Add to the onclick attribute of the submit button, two functions in which u can set the form action as the programs u have coded in php.

The functions must be written in JavaScript..

I can not provide you with a working code right now, but this should be possible. It's all a matter of some PHP magic ;)

Look around the net for PHP functions to send emails (There's a build in function) and figure out how to do both things at the same time. One good advice though, remember to sanitize your input! You don't want to give a possible attacker free access to your payment options or anything like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.