Log in

View Full Version : How to redirect a form T(to a Home Page)after submit



Sarahi
03-22-2011, 01:49 PM
Hello. am really new to php. and i need to redirect a simple php form to home page, after submit the form. the code of the form is as follow:


<?php


$yourEmail = "soapmaster@jabonesvioleta.com"; // the email address you wish to receive these mails through
$yourWebsite = "http://www.jabonesvioleta.com"; // the name of your website

$maxPoints = 4; // max points a person can hit before it refuses to submit - recommend 4


// DO NOT EDIT BELOW HERE

$error_msg = null;
$result = null;

function isBot() {
$bots = array("Indy", "Blaiz", "Java", "libwww-perl", "Python", "OutfoxBot", "User-Agent", "PycURL", "AlphaServer", "T8Abot", "Syntryx", "WinHttp", "WebBandit", "nicebot");

$isBot = false;
foreach ($bots as $bot)
if (strpos($_SERVER['HTTP_USER_AGENT'], $bot) !== false)
$isBot = true;

if (empty($_SERVER['HTTP_USER_AGENT']) || $_SERVER['HTTP_USER_AGENT'] == " ")
$isBot = true;

exit("Bots not allowed.</p>");
}

if ($_SERVER['REQUEST_METHOD'] == "POST") {
function clean($data) {
$data = trim(stripslashes(strip_tags($data)));
return $data;
}

// lets check a few things - not enough to trigger an error on their own, but worth assigning a spam score..
// score quickly adds up therefore allowing genuine users with 'accidental' score through but cutting out real spam :)
$points = (int)0;

$badwords = array("adult", "beastial", "bestial", "blowjob", "clit", "cum",
$exploits = array("content-type", "bcc:", "cc:", "document.cookie", "onclick", "onload", "javascript");

foreach ($badwords as $word)
if (strpos($_POST['comments'], $word) !== false)
$points += 2;

foreach ($exploits as $exploit)
if (strpos($_POST['comments'], $exploit) !== false)
$points += 2;

if (strpos($_POST['comments'], "http://") !== false || strpos($_POST['comments'], "www.") !== false)
$points += 2;
if (isset($_POST['nojs']))
$points += 1;
if (preg_match("/(<.*>)/i", $_POST['comments']))
$points += 2;
if (strlen($_POST['name']) < 3)
$points += 1;
if (strlen($_POST['comments']) < 15 || strlen($_POST['comments'] > 1500))
$points += 2;
// end score assignments

foreach ($_POST as $key => $value)
$_POST[$key] = trim($value);

if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
$error_msg .= "Name, e-mail and comments are required fields. \n";
} elseif (strlen($_POST['name']) > 15) {
$error_msg .= "The name field is limited at 15 characters. Your first name or nickname will do! \n";
} elseif (!preg_match("/^[a-zA-Z-'\s]*$/", stripslashes($_POST['name']))) {
$error_msg .= "The name field must not contain special characters. \n";
} elseif (!preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i', strtolower($_POST['email']))) {
$error_msg .= "That is not a valid e-mail address. \n";
} elseif (!empty($_POST['url']) && !preg_match('/^(http|https):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i', $_POST['url']))
$error_msg .= "Invalid website url.";

if ($error_msg == NULL && $points <= $maxPoints) {
$subject = "Automatic Form Email";

$message = "You received this e-mail message through your website: \n\n";
foreach ($_POST as $key => $val) {
$message .= ucwords($key) . ": " . clean($val) . "\r\n";
}
$message .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n";
$message .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n";
$message .= 'Points: '.$points;

if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) {
$headers = "From: $yourEmail \r\n";
$headers .= "Reply-To: {$_POST['email']}";
} else {
$headers = "From: $yourWebsite <$yourEmail> \r\n";
$headers .= "Reply-To: {$_POST['email']}";

}

if (mail($yourEmail,$subject,$message,$headers)) {
if (!empty($thanksPage)) {
header("Location: $thanksPage");
exit;
} else {
$result = 'Your mail was successfully sent.';
}
} else {
$error_msg = 'Your mail could not be sent this time.';
}
} else {
if (empty($error_msg))
$error_msg = 'Your mail looks too much like spam, and could not be sent this time. ['.$points.']';
}
}
function get_data($var) {
if (isset($_POST[$var]))
echo htmlspecialchars($_POST[$var]);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>My Email Form</title>

<style type="text/css">
p.error, p.success {
font-weight: bold;
padding: 10px;
border: 1px solid;
}
p.error {
background: #ffc0c0;
color: #f00;
}
p.success {
background: #b3ff69;
color: #4fa000;
}


.contact span{
font-weight: bold;
color: #000;
font-size: .95em;
}
.contact form h4 {
color: #804000;
}
.contact form .contact-field p label {
color: #804000;
}
.contact form p #submit {
color: #804000;
}
.contact form p #reset {
color: #804000;
}
.contact form .contact-field p #howcanwelhelpyou {
color: #804000;
}
.contact h3 {
color: #804000;
}
.contact h5 {
color: #804000;
}
body {
background-repeat: repeat;
background-image: url(contact.jpg);
}
</style>
<script type="text/javascript">
function MM_validateForm() { //v4.0
if (document.getElementById){
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
} }


</script>


</head>
<body>

<div class="contact">
<h3 onfocus="MM_validateForm('name','','R');MM_validateForm('phone','','RisNum');MM_validateForm('email','','RisEmail');MM_validateForm('location','','R');MM_validateForm('c omments','','R');return document.MM_returnValue">E-mail Contact Form</h3>
<h5>All fields marked with <strong>*</strong> are required.</h5>




<form action="practica.php" method="post"><!-- ensure that action is equal to the name of the page you have places the form in. -->
<noscript><p><input type="hidden" name="nojs" id="nojs" /></p></noscript>
<h4>Personal Information</h4>
<div class="contact-field">
<p>
<label for="howcanwelhelpyou">How Can We Help You ?</label>
<select name="howcanwelhelpyou" id="howcanwelhelpyou">
<option value="chooseone">Choose One</option>
<option value="wholesale">Wholesale</option>
<option value="fundraising">Fund Raising</option>
<option value="salesrep">Sales Rep</option>
<option value="privatelabel">Private Label</option>
</select>
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>
<label for="name">*Name:</label>
<input name="name" type="text" id="name" onblur="MM_validateForm('name','','R');return document.MM_returnValue" value="<?php get_data("name"); ?>" />
</p>
<p>
<label for="phone">Phone:*</label>
<input name="phone" type="text" id="phone" onblur="MM_validateForm('phone','','R');return document.MM_returnValue" />
</p>
<p>
<label for="email">*E-mail:</label>
<input name="email" type="text" id="email" onblur="MM_validateForm('email','','RisEmail');MM_validateForm('phone','','RisNum');return document.MM_returnValue" value="<?php get_data("email"); ?>" />
</p>

<p>
<label for="location">*Location:</label>
<input name="location" type="text" id="location" onblur="MM_validateForm('location','','R');return document.MM_returnValue" value="<?php get_data("location"); ?>" />
</p>
</div>
<h4>*Message</h4>
<div class="contact-field">
<p>
<textarea name="comments" cols="38" rows="5" id="comments" onblur="MM_validateForm('comments','','R');return document.MM_returnValue"><?php get_data("comments"); ?></textarea>
</p>
</div>
<p>
<input type="submit" name="submit" id="submit" value="Send" />
<input type="reset" name="reset" id="reset" value="reset" />
</p>
</form>
</div>
</body>
</html>

fastsol1
03-22-2011, 11:02 PM
Well if you are happy with the way the form functions and looks then all you need is to assign the $thanksPage variable to where ever you want to redirect to. The code already takes this variable into consideration and will redirect if the variable has a value.
So put this at the top of the script after the opening php tag -

$thanksPage = "http://yourdomain.com/yourpage.php";
obviously change the yourdomain and yourpage to the actual info of your site.