Jump to content

Recommended Posts

Hi all,

 

I have a contact form that works fine, I just need some REALLY BASIC validation added. As long as someone types in ANYTHING into the Name and Email fields to send the form.

 

I would really appreciate some really simple validation that would work with what I have. I honestly have no idea how I got this far.

 

THANKS IN ADVANCE!

 

Here is the form code:

 

--------------

<form method="post" action="sendmailscript.php">

<fieldset>

<legend>Contact Information:</legend>

<div>

<label>Name*</label>

<input name="name" type="text" required="required"/>

<br />

</div>

<div>

<label>Company Name</label>

<input name="company" type="text"/>

<br />

</div>

<div>

<label>Phone Number</label>

<input name="phone" type="tel" />

<br />

</div>

<div>

<label>Email Address*</label>

<input name="email" type="email" required="required" />

<br />

</div>

<div>

<label>Website Address</label>

<input name="website" type="url" />

<br />

</div>

</fieldset>

<fieldset>

<legend>Preferred Meeting Type:</legend>

<div>

<label> </label>

<input type="radio" name="meeting" value="Go To Meeting" />

<span class="checkboxDesc">Set Up A Go To Meeting Online Demo</span><br />

<label> </label>

<input type="radio" name="meeting" value="Face To Face" />

<span class="checkboxDesc">Contact Me About A Face-To-Face Meeting</span><br />

<label> </label>

<input type="radio" name="meeting" value="Would Like More Information" />

<span class="checkboxDesc">More Information</span><br />

</div>

</fieldset>

<fieldset>

<legend>I'm Interested In:</legend>

<div>

<label>Request Information On:</label>

<input type="checkbox" name="services[]" value="Call Tracking Telephone Numbers" />

<span class="checkboxDesc">Call Tracking Telephone Numbers</span><br />

<input type="checkbox" name="services[]" value="Design, Print & Mail Direct Mail Packages" />

<span class="checkboxDesc">Design, Print & Mail Packages</span><br />

<input type="checkbox" name="services[]" value="Direct Mail List Purchase" />

<span class="checkboxDesc">Direct Mail List Purchase</span><br />

<input type="checkbox" name="services[]" value="Printing & Graphic Design Services" />

<span class="checkboxDesc">Printing & Graphic Design Services</span><br />

<input type="checkbox" name="services[]" value="Website Design Services" />

<span class="checkboxDesc">Website Design Services</span><br />

<input type="checkbox" name="services[]" value="QR Codes & Landing Pages" />

<span class="checkboxDesc">QR Codes & Landing Pages</span><br />

<input type="checkbox" name="services[]" value="Mobile Websites" />

<span class="checkboxDesc">Mobile Websites</span><br />

<input type="checkbox" name="services[]" value="Sales & Lead Tracking Software" />

<span class="checkboxDesc">Sales & Lead Tracking Software</span><br />

<input type="checkbox" name="services[]" value="Marketing & Sales Consulting Services" />

<span class="checkboxDesc">Marketing & Sales Consulting Services</span><br />

</div>

<div>

<label>Message</label>

<textarea name="message" id="message" rows="5" required="required" />

</textarea>

</div>

</fieldset>

<input type="submit" name="submit" value="Submit" class="button" />

</form>

______________

 

 

Here is the php code:

 

-----------

 

<?php

 

 

$EmailTo = "me@me.com";

$Subject = "Request Form";

$Name = Trim(stripslashes($_POST['name']));

$Company = Trim(stripslashes($_POST['company']));

$Tel = Trim(stripslashes($_POST['phone']));

$Email = Trim(stripslashes($_POST['email']));

$Website = Trim(stripslashes($_POST['website']));

 

$Meeting = $_POST['meeting'];

 

$Services = Implode("\n", $_POST['services']);

 

$Message = Trim(stripslashes($_POST['message']));

 

$mailheader .= "Reply-To: $Email \r\n";

 

 

// prepare email body text

$Body = "";

$Body .= "Name: ";

$Body .= $Name;

$Body .= "\n";

$Body .= "Company: ";

$Body .= $Company;

$Body .= "\n";

$Body .= "Phone: ";

$Body .= $Tel;

$Body .= "\n";

$Body .= "Email: ";

$Body .= "$Email";

$Body .= "\n";

$Body .= "Website: ";

$Body .= $Website;

 

$Body .= "\n\n";

$Body .= "Preferred Meeting Type: \n";

$Body .= $Meeting;

 

$Body .= "\n\n";

$Body .= "I'm Interested In: \n";

$Body .= $Services;

 

 

$Body .= "\n\n";

$Body .= "Message: \n";

$Body .= $Message;

 

 

 

// send email

$success = mail($EmailTo, $Subject, $Body, "From: <$Email>");

 

// redirect to success page

if ($success){

print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">";

}

else{

print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/
Share on other sites

try this and see

<?php
if(isset($_POST['submit'])){

if(empty($_POST['name']) || empty($_POST['email'])){
echo 'Name and E-mail is required';
}else{
$EmailTo = "me@me.com";
$Subject = "Request Form";
$Name = Trim(stripslashes($_POST['name']));
$Company = Trim(stripslashes($_POST['company']));
$Tel = Trim(stripslashes($_POST['phone']));
$Email = Trim(stripslashes($_POST['email']));
$Website = Trim(stripslashes($_POST['website']));

$Meeting = $_POST['meeting'];

$Services = Implode("\n", $_POST['services']);

$Message = Trim(stripslashes($_POST['message']));

$mailheader .= "Reply-To: $Email \r\n";


// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= "$Email";
$Body .= "\n";
$Body .= "Website: ";
$Body .= $Website;

$Body .= "\n\n";
$Body .= "Preferred Meeting Type: \n";
$Body .= $Meeting;

$Body .= "\n\n";
$Body .= "I'm Interested In: \n";
$Body .= $Services;


$Body .= "\n\n";
$Body .= "Message: \n";
$Body .= $Message;



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

// redirect to success page
if ($success){
 print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">";
}
else{
 print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
}
}


?>

Edited by lovephp

functions.php

 

<?php
function formError($p){
		    $errormsg = array();    
		    if($p['name'] == '') {
        $errormsg['name'] = 'Name is required!';
       }
		    if($p['email'] == '') {
        $errormsg['email'] = 'E-mail is required!';
       }else if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $p['email'])) {
			 $errormsg['email'] = 'E-mail is not valid!';
		    }
		    return $errormsg;
}
?>

 

sendmail.php

<?php
include ("functions.php");

if(count($_POST)){   	     
   $f = $_POST;					    
   $error  = formError($f);   					                         
   if(count($error) == 0) {        

$EmailTo = "me@me.com";
$Subject = "Request Form";
$Name = Trim(stripslashes($_POST['name']));
$Company = Trim(stripslashes($_POST['company']));
$Tel = Trim(stripslashes($_POST['phone']));
$Email = Trim(stripslashes($_POST['email']));
$Website = Trim(stripslashes($_POST['website']));

$Meeting = $_POST['meeting'];

$Services = Implode("\n", $_POST['services']);

$Message = Trim(stripslashes($_POST['message']));

$mailheader .= "Reply-To: $Email \r\n";


// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= "$Email";
$Body .= "\n";
$Body .= "Website: ";
$Body .= $Website;

$Body .= "\n\n";
$Body .= "Preferred Meeting Type: \n";
$Body .= $Meeting;

$Body .= "\n\n";
$Body .= "I'm Interested In: \n";
$Body .= $Services;


$Body .= "\n\n";
$Body .= "Message: \n";
$Body .= $Message;


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

// redirect to success page
if ($success){
 print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">";
}
else{
 print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
	 }    
	 }
?>
<form method="post"  action="sendmail.php">
		    <fieldset>
			    <legend>Contact Information:</legend>
			    <div>
				    <label>Name*</label>
				    <input name="name" type="text" required="required"/>
<?php
	    if($error['name'] !='')
	    {
		    echo '<div class="error_msg">'.$error['name'].'</div>';
	    }

   ?>
				    <br />
			    </div>
			    <div>
				    <label>Company Name</label>
				    <input name="company" type="text"/>
				    <br />
			    </div>
			    <div>
				    <label>Phone Number</label>
				    <input name="phone" type="tel" />
				    <br />
			    </div>
			    <div>
				    <label>Email Address*</label>
				    <input name="email" type="email"  required="required" />
<?php
	    if($error['email'] !='')
	    {
		    echo '<div class="error_msg">'.$error['email'].'</div>';
	    }

   ?>
				    <br />
			    </div>
			    <div>
				    <label>Website Address</label>
				    <input name="website" type="url" />
				    <br />
			    </div>
		    </fieldset>
		    <fieldset>
			    <legend>Preferred Meeting Type:</legend>
			    <div>
			    <label> </label>
			    <input type="radio" name="meeting" value="Go To Meeting" />
			    <span class="checkboxDesc">Set Up A Go To Meeting Online Demo</span><br />
			    <label> </label>
			    <input type="radio" name="meeting" value="Face To Face" />
			    <span class="checkboxDesc">Contact Me About A Face-To-Face Meeting</span><br />
			    <label> </label>
			    <input type="radio" name="meeting" value="Would Like More Information" />
			    <span class="checkboxDesc">More Information</span><br />
			    </div>
		    </fieldset>
		    <fieldset>
			    <legend>I'm Interested In:</legend>
			    <div>
				    <label>Request Information On:</label>
				    <input type="checkbox" name="services[]" value="Call Tracking Telephone Numbers" />
				  <span class="checkboxDesc">Call Tracking Telephone Numbers</span><br />
				    <input type="checkbox" name="services[]" value="Design, Print & Mail Direct Mail Packages" />
				  <span class="checkboxDesc">Design, Print & Mail Packages</span><br />
				    <input type="checkbox" name="services[]" value="Direct Mail List Purchase" />
				  <span class="checkboxDesc">Direct Mail List Purchase</span><br />
				    <input type="checkbox" name="services[]" value="Printing & Graphic Design Services" />
				  <span class="checkboxDesc">Printing & Graphic Design Services</span><br />
				    <input type="checkbox" name="services[]" value="Website Design Services" />
				  <span class="checkboxDesc">Website Design Services</span><br />
				    <input type="checkbox" name="services[]" value="QR Codes & Landing Pages" />
				  <span class="checkboxDesc">QR Codes & Landing Pages</span><br />
				    <input type="checkbox" name="services[]" value="Mobile Websites" />
				  <span class="checkboxDesc">Mobile Websites</span><br />
				    <input type="checkbox" name="services[]" value="Sales & Lead Tracking Software" />
				  <span class="checkboxDesc">Sales & Lead Tracking Software</span><br />
				    <input type="checkbox" name="services[]" value="Marketing & Sales Consulting Services" />
				    <span class="checkboxDesc">Marketing & Sales Consulting Services</span><br />
			    </div>
			    <div>
				    <label>Message</label>
				    <textarea name="message" id="message" rows="5" required="required" />
				    </textarea>
			    </div>
		    </fieldset>
		    <input type="submit" name="submit" value="Submit" class="button" />
	    </form>

I would strongly recommend against using the script that lovephp posted! It is completely unsafe and allows any malicious user to easily take control of it, and use it for sending spam to the entire world. From your account.

 

Instead I recommend you use something like this:

<?php
if (isset ($_POST['submit'])) {
$error = $services = array ();
$website = '';

// Define an array of the available meeting types. Make sure it starts at 1 for use in form-validation.
$availableMeetings = array (
		1 => "Set Up A Go To Meeting Online Demo",
			"Contact Me About A Face-To-Face Meeting",
			"More Information",
);

// Create the output template for the meetingsradio buttons.
$meetingsTemplate = <<<OutHTML
<input tid="inp_meeting_%1\$d" type="radio" name="meeting" value="%1\$" />
<label for="inp_meeting_%1\$d" class="checkboxDesc">%2\$s</label>

OutHTML;

// Define an array of available services. Make sure it starts at 1 for use in form-validation.
$availableServices = array (
		1 => "Call Tracking Telephone Numbers",
			"Call Tracking Telephone Numbers",
			"Design, Print & Mail Packages",
			"Direct Mail List Purchase",
			"Printing & Graphic Design Services",
			"Website Design Services",
			"QR Codes & Landing Pages",
			"Mobile Websites",
			"Sales & Lead Tracking Software",
			"Marketing & Sales Consulting Services",
);

// Define the template for the service checkboxes.
$serviceTemplate = <<<OutHTML
<label for="inp_service_%1\$d" class="checkboxDesc">%2\$s</label>
<input id="inp_service_%1\d" type="checkbox" name="services[]" value="%1\$d" />

OutHTML;

// Define the RegExp to validate names by, and make sure name is legit.
$nameRegExp = '/^[a-zA-Z\\pL][\\w\\pL \\.\'\\-]{0,60}\\z/u';
if (empty ($_POST['name']) || !preg_match ($nameRegExp, $_POST['name'])) {
	$error[] = 'Missing or invalid name. ';
}

// Validate e-mail address, to prevent IMAP header injections.
if (!$formData['email'] = filter_var ($_POST['email'], FILTER_VALIDATE_EMAIL)) {
	$error[] = "Invalid e-mail address.";
} 

// If website is set, make sure it's a valid URL.
if (!empty ($_POST['website']) && $formData['website'] = filter_var ($_POST['website'], FILTER_VALIDATE_URL)) {
	$error[] = "Invalid website-address.";
}

// If phone number was provided, make sure it's all numbers.
if (!empty ($_POST['phone']) && !$formData['phone'] = filter_var ($_POST['phone'], FILTER_VALIDATE_INT)) {
	$error[] = "Invalid phone number.";
}

// Validate the preferred meeting type.
if (!isset ($availableMeetings[$_POST['meeting']])) {
	$error[] = "Invalid meeting type.";
} else {
	$meeting = $availableMeetings[$_POST['meeting']];
}

// Loop through and validate all checked service IDs.
foreach ($_POST['services'] as $serviceID) {
	if (!isset ($availableServices[$serviceID])) {
		// Not a valid service ID, add to errors and skip to next element.
		$error[] = "$ServiceID is an invalid service ID.";
		continue;
	}

	$services[] = $serviceID;
}

// Add the "too broad to validate properly" input fields to data array.
$formData['name'] = trim ($_POST['name']);
$formData['company'] = trim ($_POST['company']);
$formData['message'] = trim ($_POST['message']);

// If any errors have been found, format them properly, generate the form, and stop parsing.
if (!empty ($error)) {
	$error = '<ul class="error"><li>'.implode ("</li>\n\t<li>", $error)."</li>\n</ul>";
	echo Gen_Form ($error, $formData);
	return;
}

// Set some meta-information about the mails.
$EmailTo = "me@me.com";
$Subject = "Request Form";
$mailheader .= "Reply-To: $Email\r\n";

// Prepare email body text
$Body = <<<OutMail
Name: {$formData['name']}
Company: {$formData['company']}
Phone: {$formData['tel']}
Email: {$formData['email']}
Website: {$formData['website']}

Preferred Meeting Type:
{$formData['meeting']}

I'm Interested In:
{$formData['services']}

Message:
{$formData['message']}
OutMail;

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

// redirect to success page
if ($success) {
	header ("Location: {$_SERVER['PHP_SELF']}send=ok'");
	die ();
}

$error = '<h1 class="error">Error</h1>'."\n".
	'<p class="error">Mail was not send correctly, please try again or contant the '.
	'administrator if the problem persists.</p>';
echo genForm ($error, $formData);
return;
}

function genForm ($message = '', $formData = array (), $radioData, $checkboxData) {
// TODO: Write the function that generates the readio, and the one for checkboxes.
//       Then display the completed form. Remember to use htmlspecialchars ()
//       around text input from the user.
}

 

I've done most of it for you, but I've left the HTML generation up to you. Should be a nice exercise, and I'm just about ready to fall asleep where I'm sitting.

 

If there's something you're wondering about, please do ask and I'm sure lots of people will help.

  • 2 months later...

Might also want to use an array for all of this stuff

 

$EmailTo = "me@me.com";

$Subject = "Request Form";

$Name = Trim(stripslashes($_POST['name']));

$Company = Trim(stripslashes($_POST['company']));

$Tel = Trim(stripslashes($_POST['phone']));

$Email = Trim(stripslashes($_POST['email']));

$Website = Trim(stripslashes($_POST['website']));

 

$Meeting = $_POST['meeting'];

 

$Services = Implode("\n", $_POST['services']);

 

$Message = Trim(stripslashes($_POST['message']));

 

$mailheader .= "Reply-To: $Email \r\n";

Timothy: What do you mean? Are you saying that he should have used an array to store the values retrieved from the POST array, or something else?

 

In any case, why would you pick the absolute worst implementation to quote from?

It is appreciated that you're trying to help, but please ensure that the help you're giving is using the best available code. Otherwise you're not helping, but quite the opposite. Thank you.

 

What I meant was for all of these that he is giving individual variable names why not just use an array? In response to "is using the best available code" That was his code that was his code that I was suggesting he used an array for. Example array('EmailTo' => 'email value he had' .......

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.