/    Sign up×
Community /Pin to ProfileBookmark

PHP form validation && redirecting.

ok, I have a form and, how would I validate it so that is all the boxes are not filled in correctly then, they are alerted and, redirected to a file called thankyou.php but, if it is wrong then it sends then to the original page?

These are the codes:
This is the form *called index.php*

[code=php]

<?PHP include (“../online.php”); ?>
<html>
<head>
<title>Flexible Web Design – Contact</title>
<META HTTP-EQUIV=”imagetoolbar” CONTENT=”no”>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″>
<link rel=”stylesheet” href=”/css.css” type=”text/css”>
<script type=”text/javascript”>
<!–
function checkform ( form )
{
if (form.name.value == “”) {
alert( “This is a required field. Please enter your full name.” );
form.name.focus();
return false ;
}
if (form.email.value == “”) {
alert( “This is a required field. Please enter your email address.” );
form.email.focus();
return false ;
}
if (form.message.value == “”) {
alert( “This is a required field. Please enter your message.” );
form.message.focus();
return false ;
}
return true ;
}
//–>
</script>
</head>

<body>
<h1>Contact Us<br><img src=”/images/page_line.jpg” width=”300″ height=”12″ alt=””></h1>
Use the form below to send us questions, comments, or even to get a customized quote.<br><br>
<form name=”contact” onsubmit=”return checkform(this);” method=”post” action=”contact.php”>
* Full Name:<br><input type=”text” name=”name”><br>
* E-mail Address:<br><input type=”text” name=”email”><br>
* Subject:<br><select name=”subject”><option>Question</option><option>Comment</option><option>Quote Request</option><option>Suggestion</option></select><br>
* Message:<br><textarea name=”message” rows=”4″ cols=”20″></textarea><br>
*=Required Field<br><input type=”submit” value=”Send Message” name=”submit”></form>
</body>

</html>
[/code]

This is the contact file *called contact.php*:

[code=php]
<?PHP
$to = “[email protected]”; #set address to send form to
$subject = Flexible Web Design – Form Results”; #set the subject line
$headers = “From: [email][email protected][/email]”; #set the from address
$forward = 1; # redirect? 1 : yes || 0 : no
$location = “thankyou.php”; #set page to redirect to, if 1 is above

$date = date (“l, F jS, Y”);
$time = date (“h:i A”);

$msg = “Below is the result of your contact form. It was submitted on $date at $time.nn”;

foreach ($_POST as $key => $value) {
if ($key!=”submit”) $msg .= ucfirst ($key) .” : “. $value . “n”;
}

mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header (“Location:$location”);
}
else {
echo (“Thank you for submitting our form. We will get back to you as soon as possible.”);
}

?>
[/code]

and, the file to be redirected to if the inputs are valid is called thankyou.php

If anyone can do this then plase help – I have no experience with PHP.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@Paul_JrJan 24.2004 — This looks oddly like the exact script Ryan wrote [URL=http://webdevfaqs.com/php.php#mailer]here[/URL], but the copyright notice isn't there...
Copy linkTweet thisAlerts:
@sciguyryanauthorMar 07.2004 — Actually - I was given this script to post and, am only just learning PHP - so I dont know.
×

Success!

Help @sciguyryan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...