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
https://forums.phpfreaks.com/topic/196169-send-mail-help/
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
https://forums.phpfreaks.com/topic/196169-send-mail-help/#findComment-1030161
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
https://forums.phpfreaks.com/topic/196169-send-mail-help/#findComment-1030165
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
https://forums.phpfreaks.com/topic/196169-send-mail-help/#findComment-1030180
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
https://forums.phpfreaks.com/topic/196169-send-mail-help/#findComment-1030201
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.