Jump to content

Validation


music_fan01

Recommended Posts

I am really new to using php validation, I think I may be on to what I am looking for but not very sure. I am trying to validate my form fields just incase someone forgets (name, subject, message, and email).  Here is what I have so far. I was looking at an example on how to validate a phone number.

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>


<?php

$first_name=$_POST['name'];
$email_address=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['text'];



  if(isset($_GET['submit'])) {
          if(preg_match("/^\(([
                                  $_GET['email']) != ) {
               echo "The email field was invalid<BR>";
          }

  else if(isset($_GET['submit'])) {
          if(preg_match("/^\(([
                                  $_GET['name']) != "") {
               echo "The name field was invalid<BR>";
}
else if(isset($_GET['submit'])) {
          if(preg_match("/^\(([
                                  $_GET['subject']) != "") {
               echo "The subject field was invalid<BR>";
     } else {

mail("[email protected]","Subject: $subject",
$message, "From: $first_name <$email_address>");

echo "Thank you for using our mail form.<br/>";
echo "Your email has been sent.";

}

?>



</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/246878-validation/
Share on other sites

This is what I gathered from a few examples that I found, but when I run it, no error messages show up.

 

 


<?php

$first_name=$_POST['name'];
$email_address=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['text'];

//Determine if the sumbit button has been clicked. If so, begin validating form data.
   
  if ($_POST['submit'] == "Submit")
   {
     //Determine if a Name was entered
     
	$valid_form = true;
     
	if ($_POST['name'] == "")
	{
		echo "Enter your name";
		$valid_form = false;
	}

	else
	{
		$first_name = $_POST['email'];
	}		

	if ($_POST['email'] == "")
	{
		echo "Enter a valid email address";
		$valid_form = false;
	}

	else
	{
		$email_address = $_POST['email'];
	}
	if ($_POST['subject'] == "")
	{
		echo "Enter a subject";
		$valid_form = false;
	}

	else
	{
		$subject = $_POST['subject'];
	}

	if ($_POST['text'] == "")
	{

		echo "Enter a message";
		$valid_form = false;

	}

	elseif (strlen($_POST['text']) < 4)
	{

		echo "Enter a message";
		$valid_form = false;
	}

	else
	{
		$message = $_POST['text'];
	}


	//if all form fields were submitted properly, begin processing

	if($valid_form == true)
	{


mail("[email protected]","Subject: $subject",
$message, "From: $first_name <$email_address>");

echo "Thank you for using our mail form.<br/>";
echo "Your email has been sent.";


	}



}

?>


Link to comment
https://forums.phpfreaks.com/topic/246878-validation/#findComment-1267888
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.