Jump to content

Mailing Form


James Dean

Recommended Posts

First off, hello.

This is my first pHp code, and still tying to get a grasp on it all. This is the pHp i have for my mailing form.

Now i wish to make the user have to put in a valid email address. Except it isn't working for me. The following script isn't coming up with any errors, but it is still letting the email field be submitted with any text.

 

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

{

$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];

$errorstring = "";

if (!$name)
$errorstring = $errorstring."Name<br>";

if (!$email)
$errorstring = $errorstring."email<br>";

	if (!$comments)
	$errorstring = $errorstring."comments<br>";

	if ($errorstring!="")
		echo "You missed the following:<br>$errorstring";
	else
	{			

		$webMaster = "[email protected]";
		$emailSubject = "Contact Us Form";
		$mail_from = "$email";

		$name = $_POST['name'];
		$email = $_POST['email'];
		$comments = $_POST['comments'];

			function valid_email($email) {
  				if(eregi("^[\w\-]+?\@\w+?\.\w+$", $email)) {
   					 return TRUE;
  				} else {
   					 return FALSE;
  				}
		}

			if (!valid_email($email)) {
  				die('bad email');
}

			$body = '
			<br>
			Name
			' . $name . '
   				<br><br>
			Email
			' . $email . '
			<br><hr><br>
			Comments<p>
			' . $comments . '
			';

			$headers = "From: $email\r\n";
			$headers .= "Content-type: text/html\r\n";
			$success = mail($webMaster, $emailSubject, $body, $headers);


				$redirect = file_get_contents('redirect.html');
				echo $redirect;


	}	
}
  
?>

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/218005-mailing-form/
Share on other sites

First, it's PHP, not pHp..or you can use the common php.

 

Right - few issues:

 

Don't do this:

 

$errorstring = $errorstring."Name<br>";

 

Do this instead:

 

$errorstring .= "Name<br>";

 

Same thing.

 

Don't indent *everything* after your IF statement. Only indent what is within your if statement.

 

if (!$name)
$errorstring = $errorstring."Name<br>";






if (!$email)



$errorstring = $errorstring."email<br>";

 

..should be:

 

if (!$name)
  $errorstring = $errorstring."Name<br>";

if (!$email)
  $errorstring = $errorstring."email<br>";

//etc.

 

Your function is declared deep within your statements, that can't be good. (didn't even know it was possible).

 









function valid_email($email) {
  









if(eregi("^[\w\-]+?\@\w+?\.\w+$", $email)) {
   











return TRUE;
  









} else {
   











return FALSE;
  









}

 

That should be defined well before all of this right at the top.

 

..and your validation for email is:

 

if(eregi("^[\w\-]+?\@\w+?\.\w+$", $email))

 

So if something is still validating when it should, it is down to that because you are correctly passing the email to the function and returning as you should.

Link to comment
https://forums.phpfreaks.com/topic/218005-mailing-form/#findComment-1131558
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.