Results 1 to 6 of 6

Thread: PHP Header Question with PayPal

  1. #1
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP Header Question with PayPal

    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

    HTML Code:
    <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.

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,969
    Thanks
    16
    Thanked 307 Times in 305 Posts
    Blog Entries
    11

    Default

    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.
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    This:
    PHP Code:
    ?>
    <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?

  4. #4
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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/9...-like-download I added the code under "answers" and cannot get that to work as is.

  5. #5
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,521
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Quote Originally Posted by traq View Post
    This:
    PHP Code:
    ?>
    <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.

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by keyboard1333 View Post
    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.

    Quote Originally Posted by Jisworking View Post
    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/9...-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'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.

    Edit: Something like so (tested only in Chrome. guaranteed to break in IE<9):
    HTML Code:
    <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.
    Last edited by traq; 12-07-2012 at 02:32 AM.

Similar Threads

  1. Accordion header anchor link work while header is expanded
    By ngguh in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 05-10-2012, 07:11 PM
  2. Newb Header Question
    By chai714 in forum CSS
    Replies: 1
    Last Post: 09-30-2009, 02:38 AM
  3. Header Buttons Question
    By theflyingminstrel in forum CSS
    Replies: 1
    Last Post: 04-03-2009, 02:25 AM
  4. Paypal Question
    By cancer10 in forum The lounge
    Replies: 2
    Last Post: 02-18-2008, 11:26 AM
  5. Just a question with Paypal and PHP
    By Tristan S.S. in forum PHP
    Replies: 0
    Last Post: 02-19-2007, 03:47 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •