Jump to content

Problem with a request form using mail() function


Wolverine68

Recommended Posts

Can't get a simple request form with mail() function to work.  When clicking on the Submit button, it goes to the feedback.php file but just shows a blank page. It doesn't display the "Thank you, your request has been submitted" message along with the date and time which I included in the coding.

 

HTML form:

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html>

<head>
<title>Request Form</title>
</head>

<body background="#EEEEEE">
<h3 align="center">Request to Add to E-Mail List</h3>
<div>
<form action="feedback8.php" method="post">

<p><font color="blue">If you'd like to be added to the e-mail list, simply fill in your name and e-mail address, then click "Submit"</font></p>
<hr width="100%">
<p>Name:&nbsp&nbsp&nbsp&nbsp&nbsp<INPUT TYPE="text" SIZE="35" name="name"></p>
<p>E-mail:&nbsp&nbsp&nbsp&nbsp<INPUT TYPE="text" SIZE="35" name="email"></p>
<br><br>
<input type="submit" name="submit" value="Submit"><br><br>
<hr width="100%">

</div>
</body>

</html>


 

PHP file:

 


<?php
$formBody="Name:$name\nEmail:$email";
$headers = "From:$email";

if ($submit) {
mail("[email protected]", "Add to E-mail List Request", $formBody, $headers);
}
if ($submit) {
print "Thank you. Your request has been submitted <br /> <br />";
print "Current date and time :";
print date("F j, Y  g:i A T");
}
?>

Try the following:

 

 

<?php
/* some error checking */
error_reporting(E_ALL);
ini_set("display_errors", 1);





if(isset($_POST['submit'])                 // if someone presses submit
        && !empty($_POST['name'])     // and name is filled in
        && !empty($_POST['email'])){  // and email is filled in
//
$name = $_POST['name']; //added these
$email = $_POST['email']; // added these
$formBody="Name: $name\nEmail:$email";
$headers = "From: $email";

//        
    if(mail("[email protected]", "Add to E-mail List Request", $formBody, $headers)){  //email
        print "Thank you. Your request has been submitted <br /> <br />";
        print "Current date and time :";
        print date("F j, Y  g:i A T");
    }else{ // if the email didn't process      
       echo 'oops something went wrong';
    }
    
    
}
?>

 

The key this with forms is that you validate. Are the values as expected? (make sure you have a read on something called email header injection)

 

Also, notice i am using a $_POST variable. since your form method is 'post' we require to use those values.

 

edit: also make sure you look at the examples given in the manual:http://php.net/manual/en/function.mail.php

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.