Forum Moderators: coopster

Message Too Old, No Replies

php mail() function and post variables

I am trying to mail myself post variables from the following page...

         

shimeal

6:18 am on Mar 29, 2004 (gmt 0)

10+ Year Member



I have a page_2.php that takes the information inserted on a form on page_1.php and displays that information along with other dynamic text. The information is displayed on page_2.php so that the customer can verify it and then choose whether to submit or discard it. This page_2.php (code below) also performs some calculations on the values passed from the $_POST() from page_1.php as well as pulls values from a database. I am trying to email some of the final $variables to my email address and my submitting customer's email address (such as $total_price1). How can I make this happen since I am not posting page_2 to any other document (i.e. the data inside the form is not being posted - should it be...?)? Maybe the code can explain better...

<form name="form1" id="form1" method="post" action="">
Shipment From:

<?php echo $row_logout_template['FirstName'];?>
<?php echo $row_logout_template['LastName'];?>

E-Mail: <?php echo $row_logout_template['Email'];?>
Check Payable To: <?php echo $row_logout_template['MakeCheckPayableTo'];?>
Phone: <?php echo $row_logout_template['PhoneNumber'];?>
Fax: <?php echo $row_logout_template['FaxNumber'];?>
Address: <?php echo $row_logout_template['ShippingFromAddress'];?>
City: <?php echo $row_logout_template['ShippingFromCity'];?>
State: <?php echo $row_logout_template['ShippingFromState'];?>
Zip: <?php echo $row_logout_template['ShippingFromZip'];?><

Box Dimensions:

Length: <?php echo $_POST['Length'];?> in.
Width: <?php echo $_POST['Width'];?> in.
Height: <?php echo $_POST['Height'];?> in.
Weight: <?php echo $_POST['Weight'];?> lbs.

Cartridges Being Shipped:

<?php
$cartridge_brand1 = trim($_POST['cartridge_brand1']);
if ($cartridge_brand1 && is_integer($cartridge_brand1) && $cartridge_brand1 > 0) { /* ... SQL .... */ }
else { /* ... Error .... */ }
$cartridge_type1 = trim($_POST['cartridge_type1']);

if ($cartridge_type1 && is_integer($cartridge_type1) && $cartridge_type1 > 0)
{ /* ... SQL .... */ }
else
{ /* ... Error .... */ }
$cartridge_number1 = trim($_POST['cartridge_number1']);
if ($cartridge_number1 && is_integer($cartridge_number1) && $cartridge_number1 > 0)
{ /* ... SQL .... */ }
else
{ /* ... Error .... */ }
$cartridge_price1 = trim($_POST['cartridge_price1']);
if ($cartridge_price1)
{ /* ... SQL .... */ }
else
{ /* ... Error .... */ }
$quantity1 = trim($_POST['quantity1']);
if ($quantity1 && is_integer($quantity1) && $quantity1 > 0)
{ /* ... SQL .... */ }
else
{ /* ... Error .... */ }
?>

<bunch of selects from DB based on posted values, just echo results - jatar_k>

I need to transmit via email the data displayed in places such as (but not limited to) $row_logout_template['MakeCheckPayableTo'], $_POST['Length'], $name_brand1, print "$type1 - $number1", and $total_price1. How can I do this? Thanks in advance and sorry about such a long piece of code - not sure how to explain other than by example.

[edited by: jatar_k at 7:15 pm (utc) on Mar. 29, 2004]
[edit reason] edited code dump [/edit]

RonPK

3:12 pm on Mar 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you don't specify the script the data gets posted to (action=""), it will be sent to the default page which is the page itself. Is that what you want? Maybe it's easier to post it to a third page and let that script handle the sending of the mail.

There are generally two ways to 'remember' data across pages:
1. write the data to hidden form fields
2. store the data in session variables

httpwebwitch

3:27 pm on Mar 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I get it...

Once your page_2 has done all the necessary computations, the user sees a summary and has the option of submit or cancel. In that case, you will need a page_3, which is the "action" of the form on page_2.

If the user wants to cancel, there's no use sending any email from page_2. Create a <form action="page_3.php"> with all your data in hidden form elements. Provide your user with a <input type="submit" value="Submit"> and a <input type="button" value="Cancel" onClick="history.go(-1)">

The quickest way to pass along the entire $POST collection is using a foreach loop:
<?php
foreach ($HTTP_POST_VARS as $k=>$v){
print("<input type='hidden' name='".$k."' value='".$v."'>")
}
?>

Arriving at page_3 means the user chose to accept the transaction. It can do some hidden things like sending the confirmation email (see PHP's mail() function) to both you and the user, it can store the order details in a database, reset & empty all shopping carts, etc. It might only display a "thank you" message if all of the above are successful.

This is a valuable strategy for any multi-part forms and wizards. Good luck!

shimeal

4:39 pm on Mar 29, 2004 (gmt 0)

10+ Year Member



Your solution is exactly what I was looking for - Thank you!

shimeal

5:17 pm on Mar 29, 2004 (gmt 0)

10+ Year Member



HTTPWebWitch,

Thanks again for the reply. I have a few questions since I am not familiar with the foreach() function. First, does it need to be on page_2.php or page_3.php(sendshipmentpart4.php)? Maybe it needs to be in another part of the code on page_2.php? I have put it on page_2.php but I am not sure if I need to change $k and $v to represent actual variables in page 2 or if they are some sort of command that I don't know about? (I know these questions are elementary but I am a newbie). Do I need to put in a foreach() function for every variable on page_2.php that I want to pass? Right now, I am not able to pull up on page_3.php the variable I want to...this is the code I have for page_2.php right now (below that is the code for page_3.php):

<form name="form_check_3" id="form_check_3" method="post" action="sendshipmentpart4.php">
<?php
foreach ($HTTP_POST_VARS as $k=>$v){
print("<input type='hidden' name='".$k."' value='".$v."'>");
}
?>
<table width="525" border="0">
<tr>
<td class="shipment_heading">Shipment From: </td>
</tr>
</table>
<table width="525" border="0">
<tr>
<td align="center"><div align="center"><span class="boldtext"><?php $first_name = $row_logout_template['FirstName']; echo $first_name;?>
<input name="name_first" type="hidden" id="name_first" />
</span> <span class="boldtext"> <?php $last_name = $row_logout_template['LastName']; echo $last_name;?>
<input name="name_last" type="hidden" id="name_last" />
</span> </div></td>
</tr>
</table>

And here is the code for page_3.php (sendshipmentpart4.php):

<?php
$first_name = $_POST['name_first'];
echo $first_name;
?>

What am I doing wrong? I ran the echo test to make sure the variable was passing thru but it is not displaying on page_3.php...

httpwebwitch

8:40 pm on Mar 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



read here about foreach:
h**p://ca.php.net/manual/en/control-structures.foreach.php

The foreach loop goes through any collection or array, and allows you to do something with each item in it. The $HTTP_POST_VARS collection (sometimes shortened to just $POST) is such a beast.

BTW, this goes in page_2, inside the form that has the "submit" or "cancel" options as their only visible presence on the page.

So this line says, "look at each thing passed via POST from the previous page, one item at a time"

foreach($HTTP_POST_VARS as $k=>$v)

in that line, the $k and $v are temporary variables to which the "key" and "value" are assigned FOR EACH thing in $HTTP_POST_VARS. So if you have a form element passed in named "firstname" and the value is "John", then $k="firstname" and $v="John". The next time through the loop, $k="lastname" and $v="Smith". Then $k="phone" and $v="123-4567"

that's why the next line is:

print("<input type='hidden' name='".$k."' value='".$v."'>");

the output from that line is:

<input type='hidden' name='firstname'" value='John'>

The little foreach loop lets you take everything that was passed IN to the page, and pass it OUT again untouched. You may of course want to add some more hidden fields to your form, for the other data which you said was being computed and grabbed from databases etc.

When you're making a multipart form or "wizard", this method is truly invaluable. It lets you split a form into a sequence of pages, each page adding a few more variables to a big collection that gets parsed at the end. The final page in the sequence receives a POST collection which contains all the POSTS from all the previous pages, accumulated from page to page.

Good luck!

Did I mention you can use the same trick on $HTTP_GET_VARS, and $HTTP_SESSION_VARS? and arrays?

foreach() is your friend!

httpwebwitch

8:48 pm on Mar 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh, and can I please encourage you to use the same names for data wherever it's reused or passed around.

like, if you have a column in the database named "last_name".

then use it in your HTML forms

<input type="text" name="last_name">

and your arrays

$row['last_name']=$HTTP_POST_VARS['last_name'];

and your variables, only if you REALLY have to store stuff as variables...

$last_name = $row['last_name']

and even in specialized functions:

echo isvalid($last_name);
function isvalid($last_name){
return (strlen($last_name)>0);
}

Just a simple habit like that will save you lots of lost brain cells

:-) httpwebwitch