Jump to content

php form validate


b14green

Recommended Posts

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

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.