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 = "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!";}

?>

Link to comment
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 = "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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.