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 = "[email protected]"; $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!";} ?> Link to comment https://forums.phpfreaks.com/topic/225376-php-form-validate/ 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 = "[email protected]"; $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 Link to comment https://forums.phpfreaks.com/topic/225376-php-form-validate/#findComment-1163899 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. Link to comment https://forums.phpfreaks.com/topic/225376-php-form-validate/#findComment-1164830 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.