Log in

View Full Version : PHP Header Question with PayPal



Jisworking
12-06-2012, 06:03 PM
I am trying to make a PayPal form where if the user enters an amount they get sent to the paypal payment page and if they don't enter an amount but click submit they go to another page. I get the header to redirect fine when I add yahoo or google as the destinations but when I add the paypal link it sends me to their home page instead of the payment page. Can someone check my code and see if I'm missing something? Thanks




<form action=" <?php if(!empty($_POST['submit']))
{ if($_POST['amount'] > 0) {
header("Location: https://www.paypal.com/cgi-bin/webscr");exit;
} else {
header("Location: http://www.google.com/");exit;
}
} ?>" method="post">
<input type="hidden" name="cmd" value="_xclick"><input type="hidden" name="business" value="test@email.com">
<input type="hidden" name="item_name" value="Donation"><input type="hidden" name="item_number" value="1">
$ <input type="text" name="amount">
<input type="hidden" name="no_shipping" value="0"><input type="hidden" name="no_note" value="1"><input type="hidden" name="currency_code" value="USD"><input type="hidden" name="lc" value="AU"><input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="hidden" name="return" value="http://www.yahoo.com" />
<input type="submit" name="submit" value="Donate"><img alt="" border="0" src="https://www.paypal.com/en_AU/i/scr/pixel.gif" width="1" height="1"></form>




I also have ob_start and ob_end_flush at the beginning and end of my test page, with nothing else on the page.

Beverleyh
12-06-2012, 08:47 PM
What purpose is the form serving exactly? It almost sounds like a donation form? Where the 'submittee' chooses their own price to 'pay' you - if it is, you might be better using the actual PayPal buttons as provided in their Merchant Services area.

You can setup buy/pay now, donate and add-to-cart buttons very easily, with or without multi-choice drop down prices, and there are redirect url options on both transaction cancellation and completion. You can also use your own button images.

PayPal will provide you with the code to use in your web page too so it sounds much easier, and possibly much more reliable, than what you appear to be doing at the minute.

Of course I could have interpreted your intentions wrongly but that how its sounding at present.

traq
12-06-2012, 08:50 PM
This:
?>
<form action=" <?php if(!empty($_POST['submit']))
{ if($_POST['amount'] > 0) {
header("Location: https://www.paypal.com/cgi-bin/webscr");exit;
} else {
header("Location: http://www.google.com/");exit;
}
} ?>" method="post">
makes no sense at all, no matter what you're trying to do.

Why do you have this logic in the middle of the <form action=""> markup? Why do you have calls to header in the middle of your output? Why are you trying to redirect the user to paypal without their form?

Perhaps you could explain more clearly what it is you want to accomplish?

Jisworking
12-06-2012, 09:30 PM
Clarifying: It is a donate then download form, but donations are optional. If they want to download the file AND donate, they enter the amount and get sent to PayPal to complete the order. If they want to download but not donate AND hit the submit button by accident (I have a "just download" link as well) I want the download to happen but not go to paypal. This is going on a Wordpress page so I will add the permalinks and custom fields so the download links update automatically. I am using Google and Yahoo as tests until I can get the code to work.

It is based off of http://stackoverflow.com/questions/9339968/paypal-donate-what-you-like-download I added the code under "answers" and cannot get that to work as is.

keyboard
12-06-2012, 10:42 PM
This:
?>
<form action=" <?php if(!empty($_POST['submit']))
{ if($_POST['amount'] > 0) {
header("Location: https://www.paypal.com/cgi-bin/webscr");exit;
} else {
header("Location: http://www.google.com/");exit;
}
} ?>" method="post">
makes no sense at all, no matter what you're trying to do.

Why do you have this logic in the middle of the <form action=""> markup? Why do you have calls to header in the middle of your output? Why are you trying to redirect the user to paypal without their form?

Perhaps you could explain more clearly what it is you want to accomplish?

I believe they do not quite understand that php is only executed on page load. Is this correct Jisworking?

You'd need to take the php code that traq pointed out, and put it in a new .php file.
Then, point the submition location of the form toward that new file.

traq
12-07-2012, 02:40 AM
I believe they do not quite understand that php is only executed on page load. Is this correct Jisworking?

You'd need to take the php code that traq pointed out, and put it in a new .php file.
Then, point the submition location of the form toward that new file.
That would be the first step, yes.

To explain PHP vs. HTML further:

PHP happens server-side (i.e., on your webserver).
It runs all its code, does whatever, and sends the result (the output) to the user's browser.
Then it stops. It does not exist anymore.

HTML+CSS+JavaScript happens client-side (i.e., on the user's computer).
After the output reaches the browser, the browser parses the markup and runs any scripts.

Because of how this happens, there is no interaction at all.
PHP doesn't strictly know anything about HTML/CSS/JavaScript - it only knows that it is text that will be output to the browser.
HTML+CSS+JavaScript doesn't know anything about PHP at all.


Clarifying: It is a donate then download form, but donations are optional. If they want to download the file AND donate, they enter the amount and get sent to PayPal to complete the order. If they want to download but not donate AND hit the submit button by accident (I have a "just download" link as well) I want the download to happen but not go to paypal. This is going on a Wordpress page so I will add the permalinks and custom fields so the download links update automatically. I am using Google and Yahoo as tests until I can get the code to work.

It is based off of http://stackoverflow.com/questions/9339968/paypal-donate-what-you-like-download I added the code under "answers" and cannot get that to work as is.

Robert's solution is nonsense: if you receive the form submission, and then redirect to PayPal, PayPal will not get the form submission. (That's probably why you're being redirected to the PayPal home page - there's no payment information, so the payments page shoos you away.)

xav0989 (http://stackoverflow.com/questions/9339968/paypal-donate-what-you-like-download#answer-9361036)'s suggestion is actually a better approach. You can use javascript to check the total amount before the form is submitted, and, if there's no amount, change the action of the form away from paypal.

Something like so (tested only in Chrome. guaranteed to break in IE<9):
<form action=http://google.com id=myform>
<input name=amount>
<input type=submit value=Submit>
</form>
<script>
var form = document.getElementById( 'myform' );
form.addEventListener( 'submit',checkAmount,false );
function checkAmount(){
var amount = form.elements['amount'].value;
if( amount > 0 ){ form.action = 'http://yahoo.com'; }
}
</script>There are others on the forum who could be of more help with a javascript approach.