February 26, 2013
JQuery Select multiple paypal buttons
Question by Satch3000
I want to have 2 paypal buttons selected by 1 select box.
I have a minimum form code:
//The Form
<form id="form1" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<select name="select1" id="select1">
<option id="option1" value="1">Button1</option>
<option id="option2" value="2">Button2</option>
</select>
<input type="image" src="images/purchase_button.png" border="0" name="submit" alt="PayPal — The safer, easier way to pay online." />
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1" />
</form>
then at runtime jquery would do the rest:
$('#select1').change(function () {
if ($('option:selected', this).val() == 1) {
$('#button1').remove(); //clean up
$('#button2').remove(); //clean up
$('#form1').append('<input type="hidden" name="hosted_button_id" value="button1-code-here" id="button1" />');
} else {
$('#button1').remove(); //clean up
$('#button2').remove(); //clean up
$('#form1').append('<input type="hidden" name="hosted_button_id" value="button2-code-here" id="button2" />');
}
});
My problem is that sometimes when I select option 1 button it’s submitting button 2.
Does paypal save cookies when you go to the paypal payment page?
When is this not working smoothly everytime?
Answer by Starx
Your code needs some optimization. Make them and it will work as expected.
First use 1 button, there is no need for extra buttons
$('#select1').change(function () {
if ($('option:selected', this).val() == 1) {
$("#button1").val("button-1-code");
} else {
$("#button1").val("button-2-code");
}
});