__Pedro__ Posted July 14, 2014 Share Posted July 14, 2014 Hi, PHP beginner here. I'm trying to run a very basic script which does the following: 1) Input some data into a form 2) Outputs the data onto the screen 3) Click submit and e-mail it to me. The purpose of it is simply to learn how to input some data, output that data along with some basic calculations and then to send that data to an e-mail address. #1 & #2 I can do. I can enter some text and some numbers and on the next screen I can print a message containing the data I put into the form. Also I am taking the variables and doing some calculations which is fine. However, when I click submit (on the second screen) to e-mail the data to me, I am getting an e-mail but none of the variables have retained the data. FORM <html> <body> <form action="form2(2).php" method="post" name="PHPTEST" id="PHPTEST"> <table width="50%" border="0"> <tr> <td width="30%" scope="col"><div align="left">First Name</div></td> <th width="70%" scope="col"><div align="left"> <input type="text" name="fname" id="fname"> </div></th> </tr> <tr> <td><div align="left">Second Name</div></td> <td><input type="text" name="sname" id="sname"></td> </tr> <tr> <td>E-Mail Address</td> <td><input name="email" id="email"></td> </tr> <tr> <td>Date of Birth</td> <td><input type="date" name="dob" id="dob"></td> </tr> <tr> <td>First Number</td> <td><input type="number" name="num1" id="num1"></td> </tr> <tr> <td>Second Number</td> <td><input type="number" name="num2" id="num2"></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> </tr> </table> <p> <input type="submit"> </p> <p> </p> <p> </p> <p> </p> </form> </body> </html> SECOND PAGE OUTPUTS DATA TO SCREEN <html> <body> <form action="sendmail3.php" method="get" name="PHPTEST" id="PHPTEST"> <p> <?php $fname = $_POST["fname"]; ?> <?php $sname = $_POST["sname"]; ?> <?php $email = $_POST["email"]; ?> <?php $dob = $_POST["dob"]; ?> <?php $num1 = $_POST["num1"]; ?> <?php $num2 = $_POST["num2"]; ?> <?php $add = $num1+$num2; ?> <?php $mul = $num1*$num2; ?> <?php $div = $num1/$num2; ?> <?php $newDate = date("d-m-Y", strtotime($dob));?> <?php echo "Welcome <i>$fname $sname</i>";?> <br> <br> <?php echo "Your email address is <i>$email</i>";?> <br> <br> <?php echo "Your date of birth is <i>$newDate</i>";?> <br> <br> <?php echo "The first number you chose is <i>$num1</i>";?> <br> <br> <?php echo "The second number you chose is <i>$num2</i>";?> <br> <br> <?php echo "If you add these numbers together you get <i>$add</i>";?> <br> <br> <?php echo "If you multiply these numbers together you get <i>$mul</i>";?> <br> <br> <?php echo "If you divide $num1 by $num2 you get <i><font color=\"#FF0000\">$div</font></i>";?> <br> <br> <br> <br> Do you want to send this e-mail? </p> <p> <input type="submit" name="submit" id="submit" value="submit"> </p> <p> </p> <p> </p> </form> <p> </p> </body> </html> <?php ?> THIRD PAGE SENDS E-MAIL - BUT VARIABLES NOT HOLDING DATA. <html> <head> <title>Sending email using PHP</title> </head> <body> <?php $fname = $_POST["fname"]; ?> <?php $sname = $_POST["sname"]; ?> <?php $email = $_POST["email"]; ?> <?php $dob = $_POST["dob"]; ?> <?php $num1 = $_POST["num1"]; ?> <?php $num2 = $_POST["num2"]; ?> <?php $add = $num1+$num2; ?> <?php $mul = $num1*$num2; ?> <?php $div = $num1/$num2; ?> <?php $newDate = date("d-m-Y", strtotime($dob));?> <?php $message = "Hi, this is an email from $fname $sname"; $to = "myemail@mydomain.com"; $subject = "TEST MESSAGE FROM $fname $sname"; $headers = "From: $email \r\n"; $message = "This is simple text message from $fname $sname"; mail ($to, $subject, $headers, $message); echo "Thank you $fname $sname"; ?> </body> </html> OUTCOME I receive an e-mail saying: This is simple text message from From: But it hasn't retained $fname or $name or $email. Any ideas? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/289880-e-mailing-forms-beginner/ Share on other sites More sharing options...
hansford Posted July 14, 2014 Share Posted July 14, 2014 On the second page - you need to fill out form values so on page 3 those values are available in the $_POST array. You can do this through hidden form fields so the user doesn't see them. Quote Link to comment https://forums.phpfreaks.com/topic/289880-e-mailing-forms-beginner/#findComment-1485093 Share on other sites More sharing options...
gizmola Posted July 14, 2014 Share Posted July 14, 2014 When you send variables between scripts, you have these options: -Get parameters -Post parameters -Cookies -Session variables. In form2.php you get the return of your form, and you set these to php variables, but when the script is done running all those variables are discarded. If you really want to continue with this 3 script structure, probably the easiest way would be in script 2 to set your form with a corresponding number of hidden form variables with the same names. Then your current structure will work basically as expected. So one example would be: <?php $fname = $_POST["fname"]; ?> <input type="hidden" name="fname" id="fname" value="<?= $fname ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/289880-e-mailing-forms-beginner/#findComment-1485095 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.