Jump to content

Simple email form - Newbie


vlince

Recommended Posts

Hello all,

 

First time poster!

 

I've been asked to create a simple HTML form that submits and sends the information to a particular email.

The client is using PHP. Since I have a .NET and C# background, I've never done any PHP code in my life.

Regardless of the technology, this is a trivial task, so I opened Google and search for PHP email forms example which I did find.

 

The HTML form and the PHP code works great, it does what it needs to do but the problem I have is:

I would like to display a message, once the form is submitted, saying "Thank you for submitting your request...".

In addition, I would also like to display a red message right next to the textbox if the user has not entered an email.

 

I do not wish to go crazy with all possible validation rules and what not, this is/will be a very simple form.

The PHP code I have so far, is the following:

 

<?php

$name = $_REQUEST['txtName'];
$address = $_REQUEST['txtAddress'];
$city = $_REQUEST['txtCity'];
$email = $_REQUEST['txtEmail'];

if($email != '') 
{
    $body = "A user has submitted a form with the following:\n\n";
    $body .= "Name: $name\n";
    $body .= "Address: $address\n";
    $body .= "City: $city\n";
    $body .= "Email: $email\n";      

    mail( "[email protected]", "My nice little subject", $body, "From: $email");

	print "Thank you for submitting your request..."
}
else
{
	print "Email address is mandatory!"
}
?>

 

The <form> tag submits to itself using the following:

<form method="post" action="<?php echo $PHP_SELF;?>">

 

The <form> has 4 textboxes and one submit button.

 

If the user submits the form without entering an email address, at the top of the page the phrase "Email address is mandatory!".

But I do not wish for this message to be displayed at the top of the page, I'd like the message to be disaplyed next to the textbox in question (or underneath).

The same thing for when the form is submitted correctly, I'd like to display a message at the bottom of the <form>.

 

Looks like the "print" method isn't enough...should I have some kind of place holder <div> (or label) placed next to the textbox and make the "print" method write in that <div>.

 

As I said, this should be somewhat trivial for any one that has more then 5 min exposure to PHP.

Thanks in advance for the help!

Sincerely

 

Vince

Link to comment
https://forums.phpfreaks.com/topic/180424-simple-email-form-newbie/
Share on other sites

Try this:

-form content-
<input type="submit" name="form" value="Submit Email!">
</form>
<?php
$posted=$_POST['form'];
$name = $_POST['txtName'];
$address = $_POST['txtAddress'];
$city = $_POST['txtCity'];
$email = $_POST['txtEmail'];
$error = "";
if(isset($posted)){
	if(empty($name) || empty($address) ||empty($city) || empty($email)){
		echo "Could not complete submission: <br>";
		if(empty($name)){
			$error .="Missing Name!<br>";
		}
		if(empty($address)){
			$error .="Missing Address!<br>";
		}
		if(empty($city)){
			$error .="Missing City!"<br>;
		}
		if(empty($email)){
			$error .="Missing Email!";
		}
		echo $error;
	}
	else{
	    $body = "A user has submitted a form with the following:\n\n";
	    $body .= "Name: $name\n";
	    $body .= "Address: $address\n";
	    $body .= "City: $city\n";
	    $body .= "Email: $email\n";      

	    mail( "[email protected]", "My nice little subject", $body, "From: $email");

		echo "Thank you for submitting your request...";
	}
}
?>

 

 

Yeah you need to print or echo (doesn't matter which) the error in the HTML next to the textbox, so do something like this:

<?php
   
   $name = $_REQUEST['txtName'];
   $address = $_REQUEST['txtAddress'];
   $city = $_REQUEST['txtCity'];
   $email = $_REQUEST['txtEmail'];
   
   if($email != '') 
   {
       $body = "A user has submitted a form with the following:\n\n";
       $body .= "Name: $name\n";
       $body .= "Address: $address\n";
       $body .= "City: $city\n";
       $body .= "Email: $email\n";      
   
       mail( "[email protected]", "My nice little subject", $body, "From: $email");

      print "Thank you for submitting your request..."
   }
   else
   {
      $error = "Email address is mandatory!"
   }
?>

 

Then next to the textbox do something like this:

<?php
if($error){
?>
<div class="error">
<?php print $error; ?>
</div>
<?php
}
?>

This will only print the error div if $error exists.

Notice how we can open and close PHP blocks freely.

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.