3

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.

Before submit: enter image description here

After submit:
enter image description here

7
  • 1
    By clicking the submit button? Commented Jul 31, 2017 at 22:21
  • yea, I want the submit button to send the email. The problem is in my debugging, all of the fields (email, message, etc...) are blank. Commented Jul 31, 2017 at 22:22
  • 1
    ha ha ha.lol. but i have to agree with @Don'tPanic Commented Jul 31, 2017 at 22:22
  • your php code is written in .php or .html ? Commented Jul 31, 2017 at 22:28
  • The top page is contact.php, and the second it email-script.php Commented Jul 31, 2017 at 22:29

3 Answers 3

2

just add echo before your output variables

email: <?php echo $email ?> <br>
subject: <?php echo $subject ?> <br>
message: <?php echo $message ?> <br>
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

0

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.

1 Comment

I plan on trimming whitespace and sanitizing my data. This is just a first step in getting set up. I'll try this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.