b14green Posted January 23, 2011 Share Posted January 23, 2011 How do I make email, name and phone required fields? thanks in advance <?php $email = $_POST['email']; $name = trim($_POST['name']); $phone = trim($_POST['phone']); $time = trim($_POST['time']); $zipcode = trim($_POST['zipcode']); $date = trim($_POST['date']); $EmailTo = "myemail@somedomain.com"; $Subject = "form"; /// Add a subject $Body = ""; $Body .= "Full name:\n$name\n\n"; $Body .= "Primary phone:\n$phone\n\n"; $Body .= "time:\n$time\n\n"; $Body .= "Zip code:\n$zipcode\n\n"; $Body .= "date:\n$date\n\n"; if($Subject == NULL) {$Subject = "From $EmailFrom";} $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); if ($success){ header ('Location: confirm.html');} else{ echo "Error! Your e-mail was not sent!";} ?> Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted January 23, 2011 Share Posted January 23, 2011 <?php if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submt'])) { $email = $_POST['email']; $name = trim($_POST['name']); $phone = trim($_POST['phone']); $time = trim($_POST['time']); $zipcode = trim($_POST['zipcode']); $date = trim($_POST['date']); $newERROR=array(); if ($email=='') { $newERROR['email']=="Please enter a email address"; } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $newERROR['validEMAIL']="Please enter a valid email address"; } if ($email=='') { $newERROR['name']=="Please enter your name"; } if ($email=='') { $newERROR['phone']=="Please enter a phone number"; } if (count($newERROR)=='0') { $EmailTo = "myemail@somedomain.com"; $Subject = "form"; /// Add a subject $Body = ""; $Body .= "Full name:\n$name\n\n"; $Body .= "Primary phone:\n$phone\n\n"; $Body .= "time:\n$time\n\n"; $Body .= "Zip code:\n$zipcode\n\n"; $Body .= "date:\n$date\n\n"; if($Subject == NULL) {$Subject = "From $EmailFrom";} $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); if ($success){ header ('Location: confirm.html');} else { $strError.="Error! Your e-mail was not sent!";} } else { $strError="<div><p>Please check the following and try again:</p><ul>"; foreach ($newERROR as $error) { $strError.="<li class='indent'>$error</li>"; } $strError.='</ul></div>'; } } If (isset($strError) && strError!='') { echo $strError; } ?> This would work better if on the top of the same page as your form Quote Link to comment Share on other sites More sharing options...
b14green Posted January 25, 2011 Author Share Posted January 25, 2011 I think there was an extra bracket but I got it working. Thanks for the help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.