0

Hi, i have a form that when filled out sends and email. I want to be able to validate it, so check that all the fields are filled using PHP. I have found this website: http://www.phpjabbers.com/phpexample.php?eid=27 that i used to validate it but i am not sure how to combine the two PHP scripts to validate and then send. how would i do it?

The reason i am having trouble is both the validation and the email sending PHP require the use of "action" property.

By the way: i dont really know PHP

5
Contributors
12
Replies
44
Views
8 Years
Discussion Span
Last Post by Squidge
Featured Replies
  • You have a couple of problems here: 1) Your form action is set to [icode]$PHP_SELF[/icode], which I don't see being initialized anywhere here. So unless you have another file somewhere that you include with your first file, you need to set [icode]$PHP_SELF = $_SERVER['PHP_SELF'];[/icode] 2) $PHP_SELF (once set appropriately) will … Read More

0

what you trying to do actually is you want to validate your inputs like email id etc before sending the mail.the page you linked up here , shows the use of regular expressions to do that in php.
Now which is your other form, from two forms you mean to say which makes use of the action.
i guess in your first form user inputs his details like email id, name, some body text etc. and in the action of it specified another form, which processes those inputs and sends the mail.
If i guessed right, post your code here, against which fields you want to get validation.

0

Here is my HTML (with PHP validation)

<!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" xml:lang="en" dir="ltr" lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>
			Form
		</title>
	</head>
	<body>
<?php
	$errName     = "";
	$errAddress  = "";
	$errEmail    = "";
	$errPassport = "";
	$errPhone    = "";
	$errZip      = "";
	$errDate     = "";
	$errUser     = "";
	$errPass     = "";
	if($_POST["ac"]=="login"){
		// First Name
		if(preg_match("/^[a-zA-Z -]+$/", $_POST["fname"]) === 0)
			$errName1 = '<div class="errtext">Please enter you first name.</div>';
		// Last Name
		if(preg_match("/^[a-zA-Z -]+$/", $_POST["lname"]) === 0)
			$errName2 = '<div class="errtext">Please enter your last name.</div>';
		// Email
		if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0)
			$errEmail = '<div class="errtext">Please enter a valid email.</div>';
		// Message
		if(preg_match("/^[a-zA-Z -]+$/", $_POST["textarea"]) === 0)
			$errName3 = '<div class="errtext">Please enter a message.</div>';
	}    
?>
<form method="post" action="<?php $PHP_SELF ?>" id="commentForm" class="contact-form">
    <input type="hidden" name="ac" value="login" />
	<div class="input-holder-left">
		<label for="fname">*First Name:</label>
		<div>
			<input type="text" name="fname" class="input-short" id="fname" value="<?php echo $_POST["fname"]; ?>" />
			<?php  if(isset($errName1)) echo $errName1; ?>
		</div>
	</div>
	<div class="input-holder-right">
		<label for="lname">*Last Name:</label>
		<div>
			<input type="text" name="lname" class="input-short" id="lname" value="<?php echo $_POST["lname"]; ?>" />
			<?php  if(isset($errName2)) echo $errName2; ?>
		</div>
	</div>
	<div class="input-holder">
		<label for="email">*Email:</label>
		<div>
			<input type="text" name="email" class="input-long" id="email" value="<?php echo $_POST["email"]; ?>" />
			<?php  if(isset($errEmail)) echo $errEmail; ?>
		</div>
	</div>
	<div class="input-holder">
		<label for="textarea">*Message:</label>
		<div>
			<textarea name="textarea" class="textarea-message" id="textarea"><?php echo $_POST["textarea"]; ?></textarea>
			<?php  if(isset($errName3)) echo $errName3; ?>
		</div>
	</div>
	<div class="input-holder-submit">
		<input type="submit" value="Submit" name="submit" class="contact-submit" />
		<br />
		<br />
		<span class="required_field">(*) - Required Field</span>
	</div>
</form>
	</body>
</html>

And in a separate file the PHP mailer:

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

$to = "[email address goes here]";
$subject = "[subject]";
$first_name_field = $_POST['fname'];
$last_name_field = $_POST['lname'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$message = "
Name: $first_name_field $last_name_field
Email: $email_field
Message: $message";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: <[email address goes here]>' . "\r\n";
$headers .= '[From] <[email address goes here]>' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

include("[Result page]");
ini_set("sendmail_from","[Send from]");
ini_set("SMTP","[mail server]");
mail($to, $subject, $message, $headers);

} else {

echo "Error!";

}
?>
1

You have a couple of problems here:
1) Your form action is set to $PHP_SELF , which I don't see being initialized anywhere here. So unless you have another file somewhere that you include with your first file, you need to set $PHP_SELF = $_SERVER['PHP_SELF']; 2) $PHP_SELF (once set appropriately) will forward your form information to the same page you're on already, so your form information never actually gets sent to the page with the second set of code posted. One thing you can do to solve this is to move the code from your second page into your first page, and rearrange the PHP part so it looks like this:

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

if($_POST["ac"]=="login"){
		// First Name
		if(preg_match("/^[a-zA-Z -]+$/", $_POST["fname"]) === 0)
			$errName1 = '<div class="errtext">Please enter you first name.</div>';
		// Last Name
		if(preg_match("/^[a-zA-Z -]+$/", $_POST["lname"]) === 0)
			$errName2 = '<div class="errtext">Please enter your last name.</div>';
		// Email
		if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0)
			$errEmail = '<div class="errtext">Please enter a valid email.</div>';
		// Message
		if(preg_match("/^[a-zA-Z -]+$/", $_POST["textarea"]) === 0)
			$errName3 = '<div class="errtext">Please enter a message.</div>';
	

$to = "[email address goes here]";
$subject = "[subject]";
$first_name_field = $_POST['fname'];
$last_name_field = $_POST['lname'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$message = "
Name: $first_name_field $last_name_field
Email: $email_field
Message: $message";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: <[email address goes here]>' . "\r\n";
$headers .= '[From] <[email address goes here]>' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

include("[Result page]");
ini_set("sendmail_from","[Send from]");
ini_set("SMTP","[mail server]");
mail($to, $subject, $message, $headers);

} 
} else {

echo "Error!";   
}
	
?>

Edited by EvolutionFallen: n/a

0

in the place were it says $PHP_SELF it is supposed to be the URL to the PHP mailer. I don't know why it was like that.

What exactly am i supposed to do? im not sure if i said it above or not but i am dont really know PHP

0

You can put the URL to the second file instead of $PHP_SELF, that will work, but then you'll bypass your validation. So your options are either to put the second file into the first, as I showed above, or to put the validation code from the first into the second.

0

You can put the URL to the second file instead of $PHP_SELF, that will work, but then you'll bypass your validation. So your options are either to put the second file into the first, as I showed above, or to put the validation code from the first into the second.

I did what you did above but it did not work

0

My apologies. I misunderstood the code before, and how the error validation is working.
Try this. I didn't test it myself, but it should work. If it still doesn't, let me know what's going wrong. Also, make sure to read the comments I added on lines 13-14, 38, 39, 60, and 62:

<?php
	
	$PHP_SELF = $_SERVER['PHP_SELF'];
	$errName1    = "";
	$errName2    = "";
	$errEmail    = "";
	$errMessage  = "";

	if(isset($_POST['submit'])) {
		
		if($_POST["ac"]=="login"){
		
			$FORMOK = TRUE;	// $FORMOK acts as a flag. If you enter any of the conditionals below, 
							// it gets set to FALSE, and the e-mail will not be sent.
			
			// First Name
			if(preg_match("/^[a-zA-Z -]+$/", $_POST["fname"]) === 0) {
				$errName1 = '<div class="errtext">Please enter you first name.</div>';
				$FORMOK = FALSE;
			}
			// Last Name
			if(preg_match("/^[a-zA-Z -]+$/", $_POST["lname"]) === 0) {
				$errName2 = '<div class="errtext">Please enter your last name.</div>';
				$FORMOK = FALSE;
			}
			// Email
			if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0) {
				$errEmail = '<div class="errtext">Please enter a valid email.</div>';
				$FORMOK = FALSE;
			}
			// Message
			if(preg_match("/^[a-zA-Z -]+$/", $_POST["textarea"]) === 0) {
				$errMessage = '<div class="errtext">Please enter a message.</div>';
				$FORMOK = FALSE;
			}
			
			if($FORMOK) {
				$to = "[email address goes here]";   // Replace [] with your e-mail address -- make sure to delete the brackets too
				$subject = "[subject]";		// Same thing for subject
				$first_name_field = $_POST['fname'];
				$last_name_field = $_POST['lname'];
				$email_field = $_POST['email'];
				$message = $_POST['message'];
				
				$message = "
				Name: $first_name_field $last_name_field
				Email: $email_field
				Message: $message";
				
				$headers  = 'MIME-Version: 1.0' . "\r\n";
				$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
				
				// Additional headers
				$headers .= 'To: <[email address goes here]>' . "\r\n";
				$headers .= '[From] <[email address goes here]>' . "\r\n";
				
				// Mail it
				mail($to, $subject, $message, $headers);
				
				include("[Result page]");	   // Replace [] with the URL of your result page -- again, make sure to delete the brackets too.
				
				// I have no idea what these next 3 lines are for. You may just want to get rid of them.
				ini_set("sendmail_from","[Send from]");
				ini_set("SMTP","[mail server]");
				mail($to, $subject, $message, $headers);
			
			} else {
				echo "Error!";   
			}
		}
	}	
?>
<!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" xml:lang="en" dir="ltr" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>
		Form
	</title>
</head>
<body>
<form method="post" action="<?php $PHP_SELF ?>" id="commentForm" class="contact-form">
    <input type="hidden" name="ac" value="login" />
	<div class="input-holder-left">
		<label for="fname">*First Name:</label>
		<div>
			<input type="text" name="fname" class="input-short" id="fname" value="<?php echo $_POST["fname"]; ?>" />
			<?php  if(isset($errName1)) echo $errName1; ?>
		</div>
	</div>
	<div class="input-holder-right">
		<label for="lname">*Last Name:</label>
		<div>
			<input type="text" name="lname" class="input-short" id="lname" value="<?php echo $_POST["lname"]; ?>" />
			<?php  if(isset($errName2)) echo $errName2; ?>
		</div>
	</div>
	<div class="input-holder">
		<label for="email">*Email:</label>
		<div>
			<input type="text" name="email" class="input-long" id="email" value="<?php echo $_POST["email"]; ?>" />
			<?php  if(isset($errEmail)) echo $errEmail; ?>
		</div>
	</div>
	<div class="input-holder">
		<label for="textarea">*Message:</label>
		<div>
			<textarea name="textarea" class="textarea-message" id="textarea"><?php echo $_POST["textarea"]; ?></textarea>
			<?php  if(isset($errMessage)) echo $errMessage; ?>
		</div>
	</div>
	<div class="input-holder-submit">
		<input type="submit" value="Submit" name="submit" class="contact-submit" />
		<br />
		<br />
		<span class="required_field">(*) - Required Field</span>
	</div>
</form>
	</body>
</html>
0

Can i just say YOU ARE GOD!

But there is one small problem. The result page you get to is imputed into the page. I want it to send you to a new page. How do I do that? It says include("contact_success.php"); how do i make it so it sends you to that page?

0

You have two options:
1) You can keep the include there, and just make the page exit afterward, like so:

include("contact_success.php");
exit();

2) Alternatively, you can use a PHP redirect, which is what I usually do. Get rid of include("contact_success.php"); and instead of it place this line:

header("Location: contact_success.php");

Glad to help =)

- EF

Edited by EvolutionFallen: n/a

0

The first suggestion worked with:

include("contact_success.php");
exit();

The second did not.

The rest is working. Thanks!

-1

Hey I know this disucssion was from 3 years ago, but I just found it and have implemented the php code for my own portfolio website.

I am having an issue where the vlaidation for the message textarea works...but any type put into the messahe textarea will not show up in the send email?

I get the first and last name, from email,...but NO message?

Any help would be appreciated.

Let me know if you need any code from my end to review my issue..(same as above, so not sure of this was an issue on your end or mine?)

Daniel Speckman

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.