Forum Moderators: coopster

Message Too Old, No Replies

Submit Form and Email

how do I do both?

         

txbakers

3:00 am on Sep 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm brand new to PhP. Helping a friend install a Credit Card gateway.

I'm able to get the CC gateway working, but I also want to to email the pertinent purchase data to him, so he knows there is a sale.

He's on a FreeBSD box so no ASP and no JSP (I *know* those languages) so PhP is my only alternative (or CGI).

There is a built-in mailer function which works with CGI, but it needs to redirect.

Any ideas on how I can do this with one or two pages?

My idea was to have the validate javascript for the CC form populate the fields of a hidden form which will submit the email, then return to the same page with some type of hidden value. The page should read the post information, and if the hidden value is present, submit the CC form using the POST data.

is this possible, and how would I code it?

Thanks

willybfriendly

4:09 am on Sep 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will want to use the mail() function. It is posible to do all of this on one page. Begin with defining your entire form as a variable

$formBlock = "
<FORM action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<INPUT type=\"text\" name=\"first_name\" value=\"$_POST[first_name]\" size=\"25\">
<INPUT type=\"text\" name=\"last_name\" value=\"$_POST[last_name]\" size=\"25\">
etc...
<INPUT type=\"hidden\" name = \"op\" value = \"ds\">
<INPUT type=\"submit\" name=\"submit\" value=\"Submit This Form\"></TD></TR>
";

Which makes this page the object of the form action.

Then, the logic for what to do

if ($_POST[op]!= "ds")
{
echo "$form_block";//present the form if op!=ds
}
else if ($_POST[op] == "ds")
{
//error checking
//assemble message
//CC procesing
//Send CC info
mail($to, $subject, $msg, $mailheaders);

echo "<p>Thank you,<B> $_POST[first_name]</B>. We will be contacting your soon.</p>";
}

That is fast and sloppy, but may give you an idea of where to go. Add error messages, security checks, etc as needed

WBF

txbakers

4:19 am on Sep 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Willy.

Do I need delimiters on the page like in ASP/JSP? I am really a baby in php.

Question: The CC processing is also a form submit. Does this code do that?

willybfriendly

5:14 am on Sep 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure what a delimiter is in ASP.

The logic in this code is simple.

The hidden form field "op" is set to "ds" (do something) only if the form has been shown. So

if ($_POST[op]!= "ds")
{
echo "$form_block";//present the form if op!=ds
}

assures the form is shown the first time. This line

<FORM action=\"$_SERVER[PHP_SELF]\" method=\"post\">

brings the form back to this page, but now $_POST[op} == "ds" (do something), which is process the form. And when it is done processing the form and sending an email, it prints a "Thank You" message

The variable $formBlock is your form with all quotes escaped. If your credit card submission is being sent somewhere else you will need to reload all of the data from the form into a new form for submission, since you can't send the same form to two different places (as far as I know).

Now, I use this code to load a MySQL db while sending a message to the site owner (it's from a contact form). Using that approach, you could build a page that loaded a form from the db, emailed the site owner that an order was awaiting processing, and provided a link. The owner could then review the order information (make sure it wasn't from Nigeria, etc.) and hit the submit button to complete the processing.

I didn't go into any detail on checking for required fields, security manipulations, etc. I concatenate the message in the email from the form fields, e.g.:

$msg = "$_POST[firstName] $_POST[lastName]\n";
$msg .= "$_POST[streetAddy]\n";
etc.

So, I offer the bones and the logic. Can you take it from their?

WBF

txbakers

6:07 pm on Sep 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks. I did need the delimiters to make it work.

In ASP/JSP we use <% and %> to delimit a section of ASP/JSP code.

I didn't realize that in PHP you'd need the <?php and?> to block out which is the PHP and which is the HTML.

Looks good now.

jatar_k

6:10 pm on Sep 13, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<?php and?>

you can usually use just

<? ?>

txbakers

6:20 pm on Sep 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So now I can produce the form, and receive the mmail.

I want to capture the form values in the mail subject.

I had this: $msg="Name:"; and that worked.

When I put this: $msg="Name:"$_POST[name]; I got a blank screen.

The tutorial at php.net isn't worth much I'm afraid.

jatar_k

6:23 pm on Sep 13, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this
$msg = "Name:" . $_POST['name'];

you need the concatenation op in there and the proper syntax is to have single quotes around the var name.

txbakers

6:24 pm on Sep 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It all comes down to logic and syntax.....

I was trying the "&" then the "+"

I should have remembered my Perl and used the "."

D'OH!

txbakers

12:32 am on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



well, there is another similarity to JSP. Once you use a $_POST variable, you can't use it again.

So, we declare a variable, assign the value of the $_POST variable, and then use the new variable as often as we need.

But it's coming together nicely.

Thanks for the help.

willybfriendly

1:02 am on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tend to use extract($_POST) which will assign the array keys as variable names, i.e.

$_POST[firstName]

becomes

$firstName

etc.

I also tend to run the array through any filtering functions I am going to use before I use extract(), i.e stripslashes(), addslashes(), etc. It is a bit less coding. Be sure to pass the arrays into the functions by reference or you will be scratching your head wondering what you did wrong.

WBF

txbakers

4:57 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Alomst There. One more bit of help and I should have it.

In order to submit two separate forms I make a "transition" page.

The reg form shows, with the action of the form a transient page, which receives the POST and sends the mail.

That page also "shows" the hidden form and submit that, which processes the CC info and return to a third page.

it works, but now I want to clean it up and make the mail happen after the CC returns authorized. However, the CC data that returns auth or declined, doesn't include the name, address, etc.

How would I keep a handle on those variables? Is there a session object in php?

jatar_k

6:58 pm on Sep 15, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sorry for being slow tx, did you sort this?

Once you use a $_POST variable, you can't use it again.

not true but it depends what you did to it.

I think the only way would be a session, otherwise you could save it somewhere with some type of transaction id and then cross reference on return.

Session handling functions [php.net]

txbakers

8:08 pm on Sep 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks jatar, it's up and running now, but I could still tweak a bit.

Right now the page sends the email before submitting to the CC, so the recipient doesn't know if the order was good or declined. The information coming back from the CC doesn't include user information either, so I can't grab that from the POST.

I tried getting the session idea working, but I wasn't able to store the data correctly. I'm sure I'll get it, then I can pass the missing information via the Session object and print out a nice receipt, and do the emailing after the order was approved.

I don't want to use a DB at this time.

jatar_k

8:26 pm on Sep 15, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The session should work just fine but they seem to be a bit of a pain the first time you use them. I remember having some strange behaviours when I first used them.