Reputation: 5569
I have a form that captures and forwards a user's name, email, and a comment.
I receive the email in my inbox but the values are blank.
Example of the email received:
Name:
Email:
Comment:
Here is the code on my index.php file
<?php
$name="";
$from="";
$message="";
if(isset($_POST['submit'])){
$name=mysql_real_escape_string($_POST['name']);
$from=mysql_real_escape_string($_POST['email']);
$message=mysql_real_escape_string($_POST['comments']);
$to="[email protected]";
include("library/send_email.php");
}
?>
<form id="contactform" name="contactform" action="" method="post">
<input type="text" id="name" placeholder="Name" name="name" value="<?php echo $name;?>" required>
<input type="text" id="email" placeholder="Email" name="email" value="<?php echo $from;?>" required>
<textarea id="comments" name="comments" placeholder="Message" required><?php echo $message;?></textarea>
<button name="submit" id="submit" class="contact-btn-submit" type="submit">Send</button>
</form>
There is also a send_email.php file which includes the following:
<?php
$send_email= new class_email_sender($name,$from,$to,$message);
$send_email->send_email();
?>
Why do the emails not include the user's name, email, or comments?
Upvotes: 2
Views: 58
Reputation: 9034
If you have assigned the variables(before if statement) to avoid the PHP errors, do it this way to avoid conflicts etc.
$name=isset($_POST['name']) ? mysql_real_escape_string($_POST['name']) : "";
$email=isset($_POST['email']) ? mysql_real_escape_string($_POST['email']) : "";
$message=isset($_POST['comments']) ? mysql_real_escape_string($_POST['comments']) : "";
Also note what jeroen said. Use mysqli
instead.
Upvotes: 1
Reputation: 91742
This:
$name=mysql_real_escape_string($_POST['name']);
$from=mysql_real_escape_string($_POST['email']);
$message=mysql_real_escape_string($_POST['comments']);
As you don't have a valid mysql_*
connection, mysql_real_escape_string
returns false
so all your values are false
which results in empty strings when you send your mail.
Note that you should not use this function for anything other than preparing data to be inserted in a database and even then you should switch to PDO or mysqli and prepared statements as the mysql_*
functions are deprecated and have been removed from php 7.
Upvotes: 2