PHP multi-form process & submit

Posted on 2005-03-14
Medium Priority
Last Modified: 2008-04-26
I have a PHP page with two forms.  One form collects registration information and performs (server side) validation of that form.  When the form validates, it is presented to the visitor as a summary.  When this occurs, a second form (with hidden values) is also populated with information to submit to PayPal to collect registration fees.  

What I need to do is allow the user to hit one "submit" button of some sort and have the information from the registration form be sent by e-mail to an address, and the PayPal form data be submitted to PayPal to collect payment.

My basic question is whether there is a way to "submit" form data via pure PHP code.  Javascript isn't an option.
Question by:Syncubus
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13536274
The problem is that the form that you post to paypal needs to go directly to paypal, so your script doesn't get a look in. The way I deal with this is not to email the results until the transaction returns from paypal, at which point you know that the transaction has been completed. otherwise you don't know if the transaction was completed or not and you'll generating excess emails.

Alternatively, submit all the info to your script, send the email, then redirect to paypal with all the params attached. I think you'd have to do that using a GET, and paypal submissions may be too big to work reliably. I'm not sure how you can redirect using a POST.

CURL is not an option as it denies user access to the web interface of paypal.

Author Comment

ID: 13652531
I have discovered the code I was looking for on PayPal's site in their developer's documentation.  It is built writing to sockets.  

e.g. (super condensed):

$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);

fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13653637
This is more or less what I was referring to when I mentioned CURL, which can do that kind of thing more easily than plain sockets will, but I'm pretty sure that's not what you're asking for. The problem I was referring to is that if you start a paypal session like that, you can't hand over interaction to the user later on, i.e. once you have started talking to paypal via your script, you can't let your client carry it on from the browser. It's as if you'd logged into a site, stuck a few things in a shopping basket and then expect me to take over the session in a different browser on a different computer - it's very unlikely to work, let alone securely.

The usual situation (as I'm guessing you have) is that you pre-populate the paypal-specific fields in your form with what they have bought, prices, quantities, your paypal ID etc. then hand it over to paypal, where the user enters their paypal login details, completes the transaction and then returns to your site.

As I said, the problem with this arrangement is that you don't get a look-in as you pass the final data to paypal, though paypal will 'call you back' when it has finished. Also, are you sure you want to send an email when this form is submitted as it does not guarantee that the transaction is complete, only that the purchase info has been transferred to paypal - they may fail to log in, or just give up on the transaction. You will have to check your emails against paypal's transaction logs in order to eliminate any abandoned transactions.
Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands


Author Comment

ID: 13658355
Thank you for your suggestions.

Yes, I am absolutely sure this is the way things need to be handled, as the registration infrmation is more data than PayPal can (or needs to) handle.  PayPal only needs the total.  I thank them on the return page.

Thank you or your efforts.  FYI, the PayPal developers documentation gives some good PHP code examples, in case you're loking for some.
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13659907
Fair enough. In that case, I'd recommend that you do look at CURL instead of fsockopen - it will be simpler, faster and more reliable. It will also cope with redirects that paypal may throw at you that are very complex to handle manually, SSL, cookies, etc.


Failing that (for example if you don't have curl compiled in), you can use PEAR's HTTP_Client class:


Either way you will get much better reliability than fsockopen.

Author Comment

ID: 13661832
Actually, I ended up with two pages.  A confirmation screen that mails the results of the registration (with all of its information), and another page that builds the PayPal form [click here to pay].  

Accepted Solution

OzzMod earned 0 total points
ID: 13686558
Closed, 500 points refunded.
Community Support Moderator (Graveyard shift)

Expert Comment

ID: 21447245
Syncubus, could you post where exactly you found that code from the PayPal developers section?  I have exactly the same problem, and i would like to see what PayPal says but can't find what you found.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question