PDA

View Full Version : PHP form landing Page Help



_sameday
05-28-2010, 05:31 PM
PHP form landing Page Help

I have a contact form up and running, I want to change the landing page the user gets once their info has been submitted. I wasn't able to get the landing page to work, so for the meantime I set up an echo. Below is the code. Can anyone help me set up the landing page to you to a /thankyou.htm page? Thanks for the help.

<?php

//--------------------------Set these paramaters--------------------------

$subject = 'Request more info';

$emailadd = 'info@sameday-usa.com';

$url = 'http://sameday-usa.com/thankyou.html';


$req = '0';



// --------------------------Do not edit below this line--------------------------

$text = "Results from form:\n\n";

$space = ' ';

$line = '

';

foreach ($_POST as $key => $value)

{

if ($req == '1')

{

if ($value == '')

{echo "$key is empty";die;}

}

$j = strlen($key);

if ($j >= 20)

{echo "Name of form element $key cannot be longer than 20 characters";die;}

$j = 20 - $j;

for ($i = 1; $i <= $j; $i++)

{$space .= ' ';}

$value = str_replace('\n', "$line", $value);

$conc = "{$key}:$space{$value}$line";

$text .= $conc;

$space = ' ';

}

mail($emailadd, $subject, $text, 'From: '.$emailadd.'');

echo '<h1>Thank You!</h1>'.'<p>Your info has been received. We will contact you shortly.</p>';

?>

djr33
05-28-2010, 05:40 PM
There are two ways to do this:

1. Use header() (http://php.net/manual/en/function.header.php) to give a redirect. This must be output before any text. This means you can only do it if there are no errors displayed to the user.

2. Move your form checking code to the 'landing page'. Use thankyou.php as the action of the form. Done. Basically that's what you did above.
This may be more complex if you want error checking: they need to return to the first page. You could have them use the back arrow.
If not, you can merge all three into a single page:
Display the form if no post data was sent.
If post data was sent, check the data.
If the data is wrong (if there's an error), display the form again (+error, +echo the submitted data back into the fields).
If the data is correct, send the email and display thank you.


The more complex method I just described is the best way to do it and allows the most freedom. It does, however, require a fairly strong understanding of how PHP works. It's not that it uses anything particularly advanced, but if you are having trouble understanding the general layout of PHP code it won't be easy for you to write it. Mostly, it's just a lot of code and conditionally output HTML blocks.