Making Form Field Mandatory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
WestCoast?'s
Forum Newbie
Posts: 1
Joined: Thu Jan 06, 2011 5:58 pm

Making Form Field Mandatory

Post by WestCoast?'s »

I've been trying to make my contact form engine mandatory for the name, phone number and email fields, but when I upload it on the ftp and test it, I keep getting errors. Here's the code I have that is working without the fields being mandatory:

Code: Select all

<?php

$EmailFrom = "example@gmail.com";
$EmailTo = "example@gmail.com";
$EmailTo = "example@gmail.com";
$EmailTo = "example@gmail.com";
$Subject = "New Lead!!!";
$Name = Trim(stripslashes($_POST['Name'])); 
$Phone_Number = Trim(stripslashes($_POST['Phone_Number'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone Number: ";
$Body .= $Phone_Number;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success) {//&& ($Phone_Number != "" || $Email != "") {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
I would like it to return something that says: "Enter Your Name/Phone Number/Email" corresponding to the appropriate field they didn't fill out. Thanks for any input you can provide!
Last edited by Benjamin on Thu Jan 06, 2011 6:13 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

Re: Making Form Field Mandatory

Post by anantha »

i could not see the form and also i could not see where you are doing validation.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Making Form Field Mandatory

Post by social_experiment »

It's easiest to check if a field has a value inside it

Code: Select all

<?php if ($_POST['name'] == '') { echo 'Empty field'; } ?>
To see if your form has been submitted by clicking the submit button

Code: Select all

<?php if (isset($_POST['buttonName'])) ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Making Form Field Mandatory

Post by spedula »

This would be much easier and better to pull off using javascript, this way you won't waste any page reloads. It's easily done with jQuery.

Example:

Code: Select all

$("#formID").submit( function() {
   if ($("#formFieldID).val() == '') {
      return false;   
   }
});
Or something very close to that. You can reverse the logic and use != in the if statement then execute some other code there and then an else statement returns false.
Post Reply