Gonzalo
Gonzalo

Reputation: 39

how to get data from an html form and use it to make an email with php?

I want to send an email from the browser, but my PHP knowledge and experience are not enough.

So I have a html form

<form>
  <textarea value="Message" required></textarea>
  <input type="text" value="Name" required>
  <input type="text" value="Email" required>
  <input type="text" value="subject" required>
  <input type="reset" value="Reset" >
  <input type="submit" value="Submit">
</form>

My question here is how to fill this php code so I can get data from the html form and then send the email.

$to = "[email protected]";
$name =
$message = 
$from = 
$headers =
mail($to,$name,$subject,$message,$headers);

Upvotes: 0

Views: 63

Answers (2)

Jorge Hess
Jorge Hess

Reputation: 550

You need to set the action in the form, and get the $_POST in PHP code, here is an example:

<form action="test.php">
  <textarea value="Message" required></textarea>
  <input type="text" name="Name" value="Name" required>
  <input type="text" name="Email" value="Email" required>
  <input type="text" name="subject" value="subject" required>
  <input type="reset" value="Reset" >
  <input type="submit" value="Submit">
</form>

//test.php file

<?php
            $to = "[email protected]";
            $name = $_POST['Name'];
            $message = $_POST['Message'];
            $from = '[email protected]';
            $headers = 'your headers';
            mail($to,$name,$subject,$message,$headers);
?>

Upvotes: 1

Alexis
Alexis

Reputation: 5831

You have to add an action and a method (POST or GET) in your form

<form action="yourpage.php" method="POST">

After that add a name attribute at all your input :

<input type="text" value="Name" name="name" required>
<input type="text" value="Email" name="mail" required>

In yourpage.php

Here the method was POST so :

$_POST['name']; //Here get the posted value in input named 'name'
$_POST['mail'];

Upvotes: 2

Related Questions