Forum Moderators: coopster
i am using this php to process a form, but rather then echo a thank you i wanted to redirect the user to a thank you url.
can anyone help on this?
<html>
<head><title>feedback form</title></head>
<body>
<?php
/* */
$email = $HTTP_POST_VARS['email'];
$name = $HTTP_POST_VARS['name'];
$phone = $HTTP_POST_VARS['phone'];
$message = $HTTP_POST_VARS['message'];
/* */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($message == "") {
echo "<h4>No message entered</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($name == "") {
echo "<h4>No name entered</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($phone == "") {
echo "<h4>No Phone Number</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}
/* */
elseif (mail($email,$name,$phone,$message)) {
echo "<h4>Thank you </h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>
thank for any help
as ever
keen to learn..
To respond to your question, I really do not see the use of being redirected to a thank you page if you can output it right after the form is submitted. Usually, you say thank you and you redirect the user to the page he previously came from...
Anyway, you will find below a more logic way to process form using few functions and variable $error_count. Firstly, you count how many errors there are and then you use the if statement to write two different pages depending of the number of errors.
Have a look and report here in case you have any problem because the script below has not been tested.
<html>
<head><title>feedback form</title></head>
<body>
<?php// FUNCTION - ALLOW A-Z, a-z, 0-9 and _
function check_field1($var){if(!preg_match("/[^A-Za-z0-9_]/",$var)) {return TRUE;} else {return FALSE;}}
// FUNCTION - ALLOW 0-9
function check1($var){if(!preg_match("/[^0-9]/",$var)) {return TRUE;} else {return FALSE;}}
// FUNCTION CHECK EMAIL
function check_email($email) {if(eregi("^[a-z0-9._-]+@[a-z0-9._-]+.[a-z]{2,4}$", $email)) {return TRUE;} else {return FALSE;}}
// ALWAYS CHECK IF POST VARIABLE EXIST, IF NOT GIVE A DEFAULT VALUE
if(isset($_POST["email"])) {$email=$_POST["email"];} else {$email="";}
if(isset($_POST["name"])) {$name=$_POST["name"];} else {$name="";}
if(isset($_POST["phone"])) {$phone=$_POST["phone"];} else {$phone="";}
if(isset($_POST["message"])) {$message=$_POST["message"];} else {$message="";}
// DEFINE A VARIABLE TO COUNT ERRORS
$error_count="0";
// CHECK VARIABLE AND INCREMENT ERROR COUNT
if (!check_email($email)) {$error_count++;}
if ($message=="" OR!check_field1($message)) {$error_count++;}
if ($name=="" OR!check_field1($name)) {$error_count++;}
if ($phone=="" OR!check1($phone)) {$error_count++;}
// IF VARIABLE CONTAIN ERRORS - SHOW ERROR MESSAGES AND FORM
if ($error_count!="0") {
echo '<br><p><b>Please correct the following';
if ($error=="1") {echo ' error:</b><br>';} else {echo ' errors:</b><br>';}
//WRITE ERRORS MESSAGES
if (!check_email($email)) {echo '<p><font color="#CC3300">Invalid email address</font></p>';}
if ($message=="") {echo '<p><font color="#CC3300">No message entered</font></p>';}
if (!check_field1($message)) {echo '<p><font color="#CC3300">Only a-z, A-Z, 0-9 are allowed.</font></p>';}
if ($name=="") {echo '<p><font color="#CC3300">No name entered</font></p>';}
if (!check_field1($name)) {echo '<p><font color="#CC3300">Only a-z, A-Z, 0-9 are allowed.</font></p>';}
if ($phone=="") {echo '<p><font color="#CC3300">No phone entered</font></p>';}
if (!check1($phone)) {echo '<p><font color="#CC3300">Phone number is not valid (only numbers are allowed)</font></p>';}
// RESHOW THE FORM
echo '<p>Instead of having a back link, you can just show the form again with the correct variable as value, so that the user can just modify the wrong datas</p>
// IF NO ERRORS FOUND - DO WHATEVER YOU LIKE
} else {
//SEND EMAIL
@mail($email,$name,$phone,$message)
//OR DO SOME QUERY STUFF (INSERT, MODIF, etc.)
//OR WRITE/REDIRECT
echo '<p>Thank you. An email has been sent to you</p>';
}
?>
</body>
</html>
Oups! Almost forgot! You may also want to read few discussions about the H tags (H1 to H6) because using the <H4> as you did is not good and not safe. H tags should only appear once per page. Do some search in WW.
Cheers
Tomda
Edit: change "preformated" code to "code" code in order to keep the fluid window
Please, in the future post to the appropriate forum in order to get the most appropriate answer. Your question should be in the HTML forum and not PHP forum.
Nonetheless, to answer shortly - Yes! You can do a lot using a simple HTML <font> tags and can do everything using stylesheet (CSS).
I suggest you do some search and have some readings about HTML tags, and especially the font tags. Once your HTML has improved, do some stuff using CSS.
Keep it up
Tomda
thanks - sorry if you felt this was the wrong forum for the post.
i didnt want help on the html, rather just needed to know if you can include html within the php echo, and as it was related to the form code you helped with, i figured the php forum would be the best place for it. ;-)
anyway, thanks for the help.
i will go away and swot up on css/html..
as ever
keen to learn..
just needed to know if you can include html within the php echo
Yes, you can... In fact, with time you will tend to write all your html in PHP echo.
Personnally, I prefer to use single quote than double quote.
e.g. echo '<p><a href="#" title="link">Link</a></p>';
is better than
echo "<p><a href='#' title='link'>Link</a></p>";
Check this as well
<?php
$note="your text is";
$note.=" here";
echo '<p>'.$note.' <a href="#" title="link">Link</a>';
?>