...

View Full Version : Posting submitted data



zach4618
07-16-2007, 01:50 AM
I'm still learning PHP so I'm not sure the best way to do this. Right now I have a contact form that redirects to a separate page once the form is submitted. What I would like to do is display the information that the user submitted right there on the page, so they can double check to see that they didn't make any mistakes. Is this possible to do with a flat file or would I need to create a database?

BWiz
07-16-2007, 04:29 AM
Well it depends on your form. If you have a form that starts something like this:



<form action="check.php" method="get">
<input type="text" name="nom" /><input type="submit" />
</form>


You can call the submitted data [in check.php] by using the following PHP code:



<?php echo $_GET['nom']; ?>


This method posts all the data in the URL - allowing the user to see it all in the address bar.


The next method is the POST method. It goes something like this:


<form action="check.php" method="post">
<input type="text" name="nom" /><input type="submit" />
</form>


You can call the data from the [check.php] page by using this PHP code:

<?php echo $_POST['nom']; ?>

See, get -> $_GET while post -> $_POST.

zach4618
07-17-2007, 06:05 AM
Doesn't seem to be working for me. I don't know if you got this from my first post but I want to display the data on the page that that the user gets redirected to, not on the page that they submitted the form from.

mlseim
07-17-2007, 05:46 PM
Zach ...

So, after the email is sent, you then display a confirmation page ...
and it's that page where the information appears?

zach4618
07-17-2007, 09:08 PM
Yes, the form is filled out, the user clicks submit, and they are redirected to a new page called success.php. I want to display the data they submitted on success.php.

StupidRalph
07-17-2007, 09:40 PM
success.php


//pseudo code

//all variables are posted to success.php from within a form on your mail page.
$to = $_POST['to']; // gets its value from a form element named "to".
$subject = $_POST['subject']; // gets its value from a form element named "subject".//
$message = $_POST['message']; // gets its value from a form element named "message".
$headers = 'From: webmaster@example.com' . "\r\n";
$headers .= 'Reply-To: webmaster@example.com' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();


if ( mail($to,$subject,$message,$headers) ) { //if mail was successfully sent
echo "<h1>Success</h1>\n";
echo "<br />\n"
echo "Your email to " . $to . " concerning " . $subject . " was successfully sent.";
}
else { //if mail could not be sent
echo "Your email to " . $to . " concerning " . $subject . " failed."
}

zach4618
07-18-2007, 05:39 AM
Can't seem to make it work. Here is what I have (simplified):

contact.php:


<?php
if (@$_POST['submitted']) {
$firstName = @$_POST['firstname'];
$lastName = @$_POST['lastname'];
//continutes with more variables, form validation

$destinationEmail = "my email address";
$emailSubject = "Contact Information";
$emailBody = "$firstName $lastName"; //shortened

if ($error=="0") {
mail ($destinationEmail, $emailSubject, $emailBody);
header ('Location: success.php');
exit ();
}
}
?>
<html>
<form action="contact.php" method="post">
<input name="firstname" type="text" />
<input name="lastname" type="text" />
//rest of inputs
<input name="submitted" type="submit" value="Submit" />
</form>
</html>



success.php:

<?php
$firstName = @$_POST['firstname'];
$lastName = @$_POST['lastname'];
//continues
?>
<html>
<p>The following information has been submitted:</p>
<p>First Name: <?php echo $firstName ?></p>
<p>Last Name: <?php echo $lastName ?></p>
</html>

Can anyone see what I'm doing wrong?

PappaJohn
07-18-2007, 06:47 AM
You are posting the input back to contact.php, therefore, the $_POST variables are not set when you get to success.php.

zach4618
07-18-2007, 06:56 AM
Is there a way to post the variables for both contact.php and success.php?

PappaJohn
07-18-2007, 06:58 AM
There's really no need. You can move the php code from contact.php to success.php and change the form action to success.php

zach4618
07-18-2007, 07:11 AM
I'm not sure what you mean. Move what part of the php code?

zach4618
07-18-2007, 07:18 AM
Wait never mind. That worked - kind of. Before, if the page only redirected if certain fields were filled and no errors were found. Now it is redirected anytime you submit, regardless of which fields are filled in.

PappaJohn
07-18-2007, 07:21 AM
contact.php:



<html>
<form action="success.php" method="post">
<input name="firstname" type="text" />
<input name="lastname" type="text" />
//rest of inputs
<input name="submitted" type="submit" value="Submit" />
</form>
</html>


success.php


<?php
if (@$_POST['submitted']) {
$firstName = @$_POST['firstname'];
$lastName = @$_POST['lastname'];
//continutes with more variables, form validation

$destinationEmail = "my email address";
$emailSubject = "Contact Information";
$emailBody = "$firstName $lastName"; //shortened

if ($error=="0") {
mail ($destinationEmail, $emailSubject, $emailBody);
header ('Location: success.php');
exit ();
}
}
?>
<html>
<p>The following information has been submitted:</p>
<p>First Name: <?php echo $firstName ?></p>
<p>Last Name: <?php echo $lastName ?></p>
</html>

Alternatively, you could do this:

contact.php:



<?php
if (@$_POST['submitted']) {
$firstName = @$_POST['firstname'];
$lastName = @$_POST['lastname'];
//continutes with more variables, form validation

$destinationEmail = "my email address";
$emailSubject = "Contact Information";
$emailBody = "$firstName $lastName"; //shortened

if ($error=="0") {
mail ($destinationEmail, $emailSubject, $emailBody);
header ('Location: success.php?firstname=' . $firstname . '&lastname=' . $lastname);
exit ();
}
}
?>
<html>
<form action="contact.php" method="post">
<input name="firstname" type="text" />
<input name="lastname" type="text" />
//rest of inputs
<input name="submitted" type="submit" value="Submit" />
</form>
</html>


success.php:


<?php
$firstName = @$_GET['firstname'];
$lastName = @$_GET['lastname'];
//continues
?>
<html>
<p>The following information has been submitted:</p>
<p>First Name: <?php echo $firstName ?></p>
<p>Last Name: <?php echo $lastName ?></p>
</html>

Although this would get messy if you are passing a lot of variables to success.php.

StupidRalph
07-18-2007, 10:37 AM
Zach, be sure you're reading these posts carefully. I've stated this information in my post earlier.

zach4618
07-18-2007, 09:50 PM
I am reading them carefully, I just didn't understand what you meant. I'm still learning PHP so I have to study the code for a while before I understand it.

zach4618
07-20-2007, 07:37 AM
Thanks to everyone who helped. I've got it figured out now.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum