Reputation: 735
I am trying to send data from HTML to PHP. Here is my html code:
<div class="content">
<form action="email-script.php" method="post">
<div class="contact-form mar-top30">
<label> <span>Full name</span>
<input type="text" class="input_text" name="name" id="name"/>
</label>
<label> <span>Email</span>
<input type="text" class="input_text" name="email" id="email"/>
</label>
<label> <span>Subject</span>
<input type="text" class="input_text" name="subject" id="subject"/>
</label>
<label> <span>Message</span>
<textarea class="message" name="feedback" id="feedback"></textarea>
</label>
<label>
<input type="submit" class="button" value="Send" />
</label>
</div>
</form>
</div>
And here is my php code:
<!DOCTYPE html>
<html>
<?php
$email=$_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["name"] . "Sent this email" . "\r\n" . $_POST["feedback"] . "Sent from this email address:" . $_POST["email"];
mail("[email protected]", $subject, $message);
//header("Location: index.html");
?>
Error, please contact Jack at ([email protected]) <br>
email: <?php $email ?> <br>
subject: <?php $subject ?> <br>
message: <?php $message ?> <br>
</html>
I was thinking the best way to do this was through a POST method. How do I go about sending all of the data from the html form to the php email script via the click of the submit button. Posted below are screenshots of the issue.
Upvotes: 3
Views: 11489
Reputation: 1426
the vars are not empty you just forgot echo
them change your code like this:
<!DOCTYPE html>
<html>
<?php
$email=$_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["name"] . "Sent this email" . "\r\n" . $_POST["feedback"] . "Sent from this email address:" . $_POST["email"];
mail("[email protected]", $subject, $message);
//header("Location: index.html");
?>
Error, please contact Jack at ([email protected]) <br>
email: <?php echo $email ?> <br>
subject: <?php echo $subject ?> <br>
message: <?php echo $message ?> <br>
</html>
Upvotes: 1
Reputation: 1258
just add echo before your output variables
email: <?php echo $email ?> <br>
subject: <?php echo $subject ?> <br>
message: <?php echo $message ?> <br>
Upvotes: 2
Reputation: 5358
In addition you can use $_SERVER['REQUEST_METHOD']
to check if email-script.php
was accessed with a post request to avoid an errors occurring if the file is accessed directly.
Just surround the php script with
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// validate the fields, send the email, etc.
}
and you're fine for the next request.
You should also validate the fields before sending the email, so noone can send you empty emails. If you want to add more security set a session or use google captcha so it's harder to submit the form multiple times consecutively.
Upvotes: 0