How to put 2 different Submit Buttons in a form (w

2019-04-14 04:24发布

问题:

I have a form where I want the user to choose 2 different way to checkout :

Let say that the user enters his informations like his address, name and credit info. Using Paypal Pro, at the end, I want him to be able to choose whether or not he wants to checkout through paypal or through direct payment ... that would lead to a second form.

By the way, with CodeIgnieter, when following a form with another one, I got the validation process of the second one that always run, I mean when I come from the first form to the second one, in the second form I will see my error messages appears, for each field even if the user didn't try to submit yet. Is there a way to avoid this bug ?

Thanks !

回答1:

Edited: see end of the answer

You should do it as you would normally:

View:

<form method="post" action="mysite.com/submit">
    <input type="text" name="name1">
    <input type="text" name="name2">
    <input type="text" name="name3">

    <input type="submit" name="sbm" value="Direct">
    <input type="submit" name="sbm" value="PayPal">
</form>

In PHP (controller probably):

if($this->input->post('sbm') == "PayPal") { 
    // do something with PayPal
} else {
    // do something with direct payment
}

Edited: If you would like the caption of the button (visible text) to differ from the value, use <button> element instead of the <input type="submit">:

<button name="payment_type" value="paypal" type="submit">I want to pay with PayPal</button>
<button name="payment_type" value="direct" type="submit">Or should I go with Direct Payment?</button>


回答2:

Use some conditions for applying the validation rules.

if(isPayPalPro())
{
    $this->form_validation->set_rules(....);
}else
{
    //validation credit card payment
}

use some condition instead of isPayPalPro().



回答3:

An alternative i personally like more is to name every submit button differently, like so:

<form method="post" action="mysite.com/submit">
    <input type="text" name="name1">
    <input type="text" name="name2">
    <input type="text" name="name3">

    <input type="submit" name="direct" value="Direct">
    <input type="submit" name="paypal" value="PayPal">
</form>

Then check which value was sent:

if($this->input->post('direct')){
    //Direct button was pressed
}
if($this->input->post('paypal')){
    //Paypal button was pressed
}