I'm still pretty new to PHP, and been gettin some help on my PHP form but I can't get past this one part, so don't know what to do! I'm trying to create a valid form that has 2 fields that are required, "Name, Email" and 1 field that is not and thats "Comments". Am I doin this the right way?
<div id="contactform"><?php
if (isset($_SESSION['error'])) {
echo $_SESSION['error'];
unset($_SESSION['error']); // this will reset the $_SESSION['error'] container
} ?>
<form method="post" action="thanks.php">
<div class="grp"><label for="name">Your Name</label><br />
<input class="" id="name" type="text" name="name" onfocus="this.style.backgroundColor='#f7ffe3'
" onblur="this.style.backgroundColor='#fff'" /></div>
<div class="grp"><label for="email">Your Email</label><br />
<input class="" id="email" type="text" name="email" onfocus="this.style.backgroundColor='#f7ffe3'
" onblur="this.style.backgroundColor='#fff'" /></div>
<div class="grp"><label for="txt">Your Message</label><br />
<textarea id="txt" name="txt" rows="" cols="" onfocus="this.style.backgroundColor='#f7ffe3'
" onblur="this.style.backgroundColor='#fff'" ></textarea></div>
<div class="grp2"><input class="formsubmit" type="submit" value="Submit" name="submit" /></div>
</form>
</div>
Above is the contact form from the contact.php form page. It said I was missing somethin in the php file so my whole contact page wouldnt load and i saw that the unset didnt have a ")" on the end so it looked like this before I added
unset($_SESSION['error'];
thanks.php is below
<?php
session_start();
// this works well with redux - ie. form action = $_SERVER['PHP_SELF']
if(isset($_POST['submit'])) {
// set a function that will clean your data
function clean($value) {
$var = strip_tags($value);
$var = trim($value);
return $var;
}
// still clean all data that is on the form
$name = clean($_POST['name']);
$email = clean($_POST['email']);
$txt = clean($_POST['txt']);
// check to see if field has been filled in at all
// this is the spot to place the *manditory fields*
// to validate that data has been entered.
// If you don't require it as a manditory field then get rid of it.
$_SESSION['error'] = NULL;
if (empty($name)) {
$_SESSION['error'] .= "You forgot to enter your name!<br />";
$name = FALSE;
}
if (empty($email)) {
$_SESSION['error'] .= "You forgot to enter your email!<br />";
$e = FALSE;
}
// if all is true then send the email
if ($name && $email) {
$to = "email@gmail.com";
$subject = "Company Form Filled";
$body = "
Name: $name\n\n
Email: $email\n\n
Body: $txt";
$sent = mail($to, $subject, $body);
if ($sent) {
header ("Location: http://www.website.com/thankyou.php");
} // end if $sent
} // end if all var are true
} // end if post submit
// then within your form page where you want
// the error message to display, have the following code:
?>
When I hit the submit button and I leave a field blank, it goes to a blank page. Is how I did this right so far? Whats the best way to report errors? I would like to use this on my other sites but don't know if I'm coding this the right way to use it over and over again.
Thanks