简体   繁体   中英

Different POST Options for form, then passing data

Currently, I am working on a site that uses PayPal to Checkout. I would like to enable stripe, as well, so there can be a "checkout with stripe" button at the bottom of the form.

<form method="POST" id="form1" >
    <select id="udidSelect" onchange="udidPrice(this)">
        <option value="invalid" disabled="disabled">Choose Package</option>
        <option value="udid">UDID Registration</option>
        <option value="profile">UDID Registration & Free Unlimited Apps (Provisioning Profile)</option>
    </select><br />
    <br />
    <input type="text" placeholder="Name" /><br />
    <br />
    <input type="email" placeholder="Email" /><br />
    <br />

    <input type="text" placeholder="UDID (40 Characters Long)" name="os0" /> <br />
    <input type="hidden" name="on0" value="UDID">
    <br />

    <input type="hidden" name="cmd" value="_s-xclick">
    <input type="hidden" name="hosted_button_id" id="udidbuttonID" value="9PSTCESRS3FSG">
    <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!" value="PayPal" onclick="submitForm('https://www.paypal.com/cgi-bin/webscr')" style="float:right;">
    <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">

</form>

Now I would like to have a PayPal Button at the bottom of the form and a Stripe button at the bottom of the form. And if the user clicked the stripe button, it would take them to the next page and PASS ALONG the information entered in this page. I am not sure how to do this, and I am also not sure how to have different post options for the form. (Sorry I am kind of new to this.)

On the stripe checkout page, also, how would I request that information that was passed along, from the previous form. Would it just be something like this?

<input type="hidden" name="somedata" value="<?php echo $_POST['somedata']; ?>" />
<input type="hidden" name="otherdata" value="<?php echo $_POST['otherdata']; ?>" />

If I'm reading your question correctly, you want the form to go to different pages depending on whether the user clicked on Stripe or Paypal.

You can use JavaScript/jQuery to change the action attribute of the form:

<!--HTML-->
<button onClick="setFormAction('stripe')">Stripe</button>
<button onClick="setFormAction('paypal')">Paypal</button>

//Javascript
function setFormAction(which) {
    if (which == 'stripe') {
        //Change 'stripe.php' to the proper URL
        document.getElementById('form1').action = 'stripe.php';
    } else {
        document.getElementById('form1').action = 'paypal.php'; //Change this also
    }
    //Finally, submit the form
    document.getElementById('form1').submit();
}

Or, more understandably, the jQuery solution:

<!--HTML-->
<button id="stripe">Stripe</button>
<button id="paypal">Paypal</button>

//Javascript
$('button#stripe').click(function() {
    $('form#form1')
        .attr('action', 'stripe.php') //Change to proper stripe URL
        .submit();
});
$('button#paypal').click(function() {
    $('form#form1')
        .attr('action', 'paypal.php') //Change to paypal URL
        .submit();
});

On the next page, you do exactly what you said previously:

<input type="hidden" name="somedata" val="<?php echo $somedata; ?>" />

Always sanitize user-inputted values before echoing them on the page (therefore the variable $somedata rather than $_POST['somedata'] .

An alternative to hidden input fields is sessions. Much easier to handle once you get the hang of it.

Add another line as:

test.php is the page where you want to post this form

<input type="button"  border="0"   value="Pay with Stripe" onclick="submitForm('test.php')">

in test.php you can get the values like this:

<input type="hidden" name="test" value="<?php echo $_REQUEST['test'] ?>">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM