2 Submit Buttons 2 Actions Same Form Data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GregTexas
Forum Newbie
Posts: 6
Joined: Mon Jan 26, 2015 2:41 pm

2 Submit Buttons 2 Actions Same Form Data

Post by GregTexas »

I'm a shade tree programmer trying solve this. I want my users to have 2 buttons giving them the option of paying by Paypal or Escrow after filling out a form.

This is the code I have now:

Code: Select all

<?php if ($items['paypal'] == '1'): ?>
     <form action='submitPaypalBuy.php' method='post' target='_top' onclick='exit = false'>
         <center><table>
                 <tr><td><font color="316331"><b>Name</b></font></td><td><input type='text' name='name' /></td></tr>
                 <tr><td><font color="316331"><b>Phone Number</b></font></td><td><input type='text' name='phone' /></td></tr>
                 <tr><td><font color="316331"><b>Email Address</b></font></td><td><input type='text' name='email' /></td></tr>
                 <tr><th colspan='2'><input type='submit' value='Submit' /></th></tr>
             </table>
     </form>
 <?php else: ?>
     <form action='submitEscrow.php' method='post' target='_top' onclick='exit = false'>
         <center><table>
                 <tr><td><font color="316331"><b>Name</b></font></td><td><input type='text' name='name' /></td></tr>
                 <tr><td><font color="316331"><b>Phone Number</b></font></td><td><input type='text' name='phone' /></td></tr>
                 <tr><td><font color="316331"><b>Email Address</b></font></td><td><input type='text' name='email' /></td></tr>
                 <tr><th colspan='2'><input type='submit' value='Submit' /></th></tr>
             </table>
     </form>
 <?php endif; ?>
Any ideas?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: 2 Submit Buttons 2 Actions Same Form Data

Post by requinix »

Combine submitPaypalBuy.php and submitEscrow.php into one file, or create one file that somehow invokes those two (like through require()), so you can have one form pointing to one location.
Then use a <button> instead of a regular <input type=submit>, which gives you the option of giving a name and value and a button label.

Code: Select all

<button type="submit" name="submit" value="paypal">Pay with PayPal</button>
To be honest, rather than combine the two, what you should be doing is moving the processing code for each into a class,

Code: Select all

class PaypalPaymentMethod {
    public function pay($name, $phone, $email) {

Code: Select all

class EscrowPaymentMethod {
    public function pay($name, $phone, $email) {
then using your single file to execute either of those.

Then you branch like

Code: Select all

if ($_POST["submit"] == "paypal") {
    $class = new PaypalPaymentMethod();
    $class->pay($_POST["name"], $_POST["phone"], $_POST["email"]);
}
Even cooler would be to use interfaces.

Code: Select all

interface PaymentMethod {
    public function pay($name, $phone, $email);
}

Code: Select all

class PaypalPaymentMethod implements PaymentMethod {
    public function pay($name, $phone, $email) {
and

Code: Select all

if ($_POST["submit"] == "paypal") {
    $method = new PaypalPaymentMethod();
} else if ($_POST["submit"] == "escrow") {
    $method = new EscrowPaymentMethod();
} else {
    // error
}

$method->pay($_POST["name"], $_POST["phone"], $_POST["email"]);
GregTexas
Forum Newbie
Posts: 6
Joined: Mon Jan 26, 2015 2:41 pm

Re: 2 Submit Buttons 2 Actions Same Form Data

Post by GregTexas »

Thanks! In your example I'm not seeing how the form data is being routed to 2 different files depending on which button is clicked.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: 2 Submit Buttons 2 Actions Same Form Data

Post by requinix »

In my example, well, the second half of my post, you're not using two files anymore but putting the important parts of their code into classes. Not the code that deals with $_POST and actually getting the name, phone, and email values, but the payment processing stuff. Whatever that is. The one file then uses one of those classes, depending on which button was pressed.

If you need to show errors, like the name is empty or something, then you should first try to deal with that in the one file. Because it's the same everywhere: gotta have a name, phone number, and email address. But special things that only the payment method would know about, like I don't know I have no idea what the code looks like, that can be stored in the object and the one file can find it.
Like the pay() method returns true (success) or false (failure), and if it fails then it sets error information, like

Code: Select all

$this->error = "Couldn't do the thing because ...";
and the one file does

Code: Select all

if ($method->pay(...)) {
    // success
} else {
    // failure
    echo $method->error;
}
If you're still not following, how about posting the code to the two files and I'll show you what I mean?
GregTexas
Forum Newbie
Posts: 6
Joined: Mon Jan 26, 2015 2:41 pm

Re: 2 Submit Buttons 2 Actions Same Form Data

Post by GregTexas »

I ended doing your first suggestion, combining the 2 files and using 2 buttons. Works great. Thanks for your help requinix.
Post Reply