form submit >> redirect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

joejoe
Forum Newbie
Posts: 7
Joined: Sat Feb 14, 2004 12:14 pm

form submit >> redirect

Post by joejoe »

Bit basic this I'm sure, looking round at the other posts. Trying to get a form on submission to go to a ready-made success page or if the fields haven't all been filled in to a different ready-made unsuccessful page.

Wonder if you could tell me what's going wrong with this? Or suggest how else I could do this. Thanks.

At the beginning of the page:

<?
ob_start();
?>

Later on:

<?
if ($submit) { // if the form was sent do the following

if($name && $subject && $email && $message ) { // if all field are filled-in send email
mail("me@mysite.com","$subject","$message","From: $name <$email>") or die("email error");
header("Location: http://yoursite.com/form_success.php");
exit();
} else {
header("Location: http://yoursite.com/form_unsuccess.php");
exit();
}
}else{
//display the form
?>
<form action="" method="post" name="contact">
Name: <br>
<input type="text" name="name">
<br>
Email: <br>
<input type="text" name="email">
<br>
Subject: <br>
<input type="text" name="subject">
<br>
Text:<br>
<textarea name="message" cols="40" rows=""></textarea>
<br>
<input type="submit" name="submit" value="Send">
<input type="reset" name="Reset" value="Reset">
</form>
<?php
}//end else
?>
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Header can only be used if nothing has been put to the browser. This includes all echos, prints, anything OUT of PHP, a blank first line...
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

so what is wrong? what is the error? have oyu even tested it to see if it works?

you might want to change this:
<form action="" method="post" name="contact">
to:
<form action="<?php= PHP_SELF ?>" method="post">
joejoe
Forum Newbie
Posts: 7
Joined: Sat Feb 14, 2004 12:14 pm

Post by joejoe »

Been trying different variations out for quite a while with different error messages actually.

For the present above, including the change to the form I get parsing errors. e.g.

Parse error: parse error in /home/mysite/public_html/faq.php on line 183 - which is in the form - <form action="<?php= PHP_SELF ?>" method="post">.

The error I get for the code with the <form action="" method="post" name="contact"> - is another parsing error which refers to the line after all the code on the page has finished.

Any more ideas would be very welcome.
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Use this instead.

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

oops, forgot the ;

<form action="<?php= PHP_SELF; ?>" method="post">
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Um..Why are you putting an equal sign in there?
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

its the same thing....
http://us4.php.net/echo

Code: Select all

echo() also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. This short syntax only works with the short_open_tag configuration setting enabled.
and im used to doing it that way in ASP...
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Ah ok...Just asking.. :)
joejoe
Forum Newbie
Posts: 7
Joined: Sat Feb 14, 2004 12:14 pm

Post by joejoe »

Sorry guys, am getting the line 232 parsing error - at the end of the page with the line <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> inserted instead of the other.

Is the top bit OK?
<?
ob_start();
?>
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

show the code around that form
joejoe
Forum Newbie
Posts: 7
Joined: Sat Feb 14, 2004 12:14 pm

Post by joejoe »

<p class="text">If you think we should add any information to this FAQ,
please do let us know as we would like it to be as helpful as possible.
Thanks.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name: <br>
<input type="text" name="name">
<br>
Email: <br>
<input type="text" name="email">
<br>
Subject: <br>
<input type="text" name="subject">
<br>
Text:
<textarea name="message" cols="40" rows=""></textarea>
<br>
<br>
<input type="submit" name="submit" value="Send">
<input type="reset" name="Reset" value="Reset">
</form>
<?
ob_start();

if ($submit) { // if the form was sent do the following

if($name && $subject && $email && $message ) { // if all field are filled-in send email
mail("me@mysite.com","$subject","$message","From: $name <$email>") or die("email error");
echo "Message Sent"; // if all went well, display message was sent
header("Location: http://www.mysite.com/form_success.php");
exit();
} else {
header("Location: http://www.mysite.com/form_unsuccess.php");
exit();
}
}else{
//display the form
?>
</td>
<td width="20" bgcolor="#FFFFFF" height="1523">&nbsp;</td>
</tr>
<tr>
<td width="600" bgcolor="#FFFFFF" colspan="2">&nbsp;</td>
</tr>
<tr valign="top">
<td colspan="4" bgcolor="#FF7700">
<div align="right"><!-- #BeginLibraryItem "/Library/copyright.lbi" --><font face="Arial, Helvetica, sans-serif" size="-1"><b>Site XX.com &copy;
2004</b></font><!-- #EndLibraryItem --></div>
</td>
</tr>
</table>
</body>
</html>
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

... what? your code is really confusing... YOu have teh form at the very top... and then down near the bottom in your if and else's you have a commented area taht says display form, but htere is no form there... what int he world is giving you the error? what line?
joejoe
Forum Newbie
Posts: 7
Joined: Sat Feb 14, 2004 12:14 pm

Post by joejoe »

Sorry about this. Code was suggested to me - should have seen what I started with!!

Parse error: parse error in /home/mysite/public_html/faq.php on line 233

line 233 is the line after all of the code on the page.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

is what you posted above the whole page? surely not, but is that the bottom of the page? if so then you need to add

Code: Select all

<?php
}
?>
to the very bottom... other than that, i dont know
Post Reply