Jump to content

Send Mail help


richard_PHP

Recommended Posts

Hi all,

 

I have a contact form on my website that I need help with. Fill out the form as usual, click submit and it sends an email with the info. Yes. But what happens should a necessary field get left out? Well then the email won't send yes? NO!

 

I've worked on the server-side page for the sendmail but am now at a loss and am wondering where I've gone wrong.

 

Basically there are 5 fields (name*, email*, business name, number* and message* (*=required)), got it HALF working, if one required field is entered then it sends the email. I want it if any field is left out then it says an error.

 

Here's the code:

 

$name = $_POST['name'];
$email = $_POST['email'];
$business = $_POST['business'];
$number = $_POST['number'];
$message = $_POST['message'];
//leave the next 2 lines alone
$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-Type: text/html; charset=ISO-8859-1rn"; 
if ($name . $email . $number . $message == "") {
echo "<p>You appear to not have filled in the necessary fields.</p>";
echo "<p>Please click <a href='contact.html'>here</a> to re-enter your information.</p>";
}

 

Is there also a way to send the user back to the form (.html page) with all their information still entered?

 

Many thanks!

Link to comment
Share on other sites

you want this in your if statement. if any of those variables are empty ( "", NULL, 0 or false) it will result in true and trigger your error message.

 

if (empty($name) || empty($email) || empty($number) || empty($message)) {

 

 

Is there also a way to send the user back to the form (.html page) with all their information still entered?

 

 

If you want the POST data still in the form you will either need to POST the form to the same page or put the data in a session then use header to redirect back to the form.

 

Easiest is to POST to the same page then do value="<?php echo $_POST['business']; ?>" in your text fields, etc.

 

Then you can put your error message in a variable and display that in the page as well.

Link to comment
Share on other sites

Here is a rough example of how you could build the Form page and the Processing page in one. This allows you to redisplay the form (with the submitted values) when there is a validation error.

 

<?php

$name = trim($_POST['name']);
$email = trim($_POST['email']);
$business = trim($_POST['business']);
$number = trim($_POST['number']);
$message = trim($_POST['message']);

$error = '';
if (isset($_POST['name']) && (empty($name) || empty($email) || empty($number) || empty($message)))
{
    $error .= "<p>You appear to not have filled in the necessary fields.</p>";
}
else
{
    //validation passed, create and send the email

   $headers .= "MIME-Version: 1.0rn";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1rn"; 
    mail($to, $subject, $message, $headers);
  
    //redirect to success page
    header('Location: http://www.example.com/message_sent.php')
    exit();
}
?>
<html>
<body>
<?php echo $error; ?>
<form action="">
*Name: <input type="text" name="name" value="<?php echo $name; ?>" /><br>
*Email: <input type="text" name="email" value="<?php echo $email; ?>" /><br>
Business: <input type="text" name="business" value="<?php echo $business; ?>" /><br>
*Number: <input type="text" name="number" value="<?php echo $number; ?>" /><br>
*Message:<br /><textarea name="message"><?php echo $message; ?></textarea><br>
<button type="submit">Send Email</button>
</form>
</body>
</html>

Link to comment
Share on other sites

Thanks for the help. 'schilly', your code worked fine. 'mjdamato'...... I've tried your code and it has come up with: Warning: Cannot modify header information - headers already sent by (output started at ****htdocs/contact.php:7) in ******htdocs/contact.php  on line 45.

 

Any help? Line 45 contains: header('Location: mail.php');

Link to comment
Share on other sites

Okay, after changing the code from the shockingly bad bit code having two opening <html> tags ( :-[ ), I now have the error message:

 

Warning: Cannot modify header information - headers already sent by (output started at /*********/htdocs/contact.php:7) in /********/htdocs/contact.php  on line 45

 

Here's the code:

 

<?php

$name = trim($_POST['name']);
$email = trim($_POST['email']);
$business = trim($_POST['business']);
$number = trim($_POST['number']);
$message = trim($_POST['message']);

$error = '';
if (isset($_POST['name']) && (empty($name) || empty($email) || empty($number) || empty($message)))
{
    $error .= "<p>You appear to not have filled in the necessary fields.</p>";
}
else
{
    //validation passed, create and send the email

   $headers .= "MIME-Version: 1.0rn";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1rn"; 
    mail($to, $subject, $message, $headers);
  
    //redirect to success page
    header('Location: mail.php');
    exit();
}
?>
<?php echo $error; ?>
<form action="">
*Name: <input type="text" name="name" value="<?php echo $name; ?>" /><br>
*Email: <input type="text" name="email" value="<?php echo $email; ?>" /><br>
Business: <input type="text" name="business" value="<?php echo $business; ?>" /><br>
*Number: <input type="text" name="number" value="<?php echo $number; ?>" /><br>
*Message:<br /><textarea name="message"><?php echo $message; ?></textarea><br>
<button type="submit">Send Email</button>
</form>

Link to comment
Share on other sites

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.