I created a contact form to send directly to my email. It sends, but does not take information from text inputs

Asked

Viewed 45 times

-1

Hello.

I created a contact form on my page. Along with this contact form, I created a PHP document for it to send to my email the information that the user typed in the text inputs of the contact form. I get the email, but I don’t get the information the user typed in. Would anyone know why?

Thank you.

<form action="Send2.php" method="post" enctype="text/plain">
<input type="text" name="nome" id="nome" size="20">
<br/>
<br/>
<input type="text" name="email" id="email" size="20">
<br/>
<br/>

<input type="submit" value="Enviar" >

</form>

<?php

$name= $_POST['nome'];
$email= $_POST['email'];

echo $name;
echo $email;
?>

<?php

$to = "[email protected]";
$subject = "Contato Site - ManaSoft";
$message = "Name: ".$name. "<br/> E-mail: ".$email. "<br/>";
$header = "MIME-Version: 1.0\n";
$header .= "Content-type: text/html; charset=iso-8859-1\n";
$header .= "From: $email\n";

mail($to, $subject, $message, $header);

?>

3 answers

0

Try it this way:

<!doctype html>
<html>

<head>
   <meta charset="UTF-8">
</head>

<body>
<form method="POST" action="seuArquivo.php">

<input type="text" name="nome" id="nome" size="20">
<br/>
<br/>
<input type="text" name="email" id="email" size="20">
<br/>
<br/>

<input type="submit" value="Enviar" >

</form>

</body>

<?php

$name= $_POST['nome'];
$email= $_POST['email'];

echo $name;
echo $email;

$to = "[email protected]";
$subject = "Contato Site - ManaSoft";
$message = "Name: ".$name. "<br/> E-mail: ".$email. "<br/>";
$header = "MIME-Version: 1.0\n";
$header .= "Content-type: text/html; charset=iso-8859-1\n";
$header .= "From: $email\n";

mail($to, $subject, $message, $header);

?>

</html>

0

PHP does not populate POST or GET with enctype="text/plain". The values of enctype may be application/x-www-form-urlencoded (standard) or multipart/form-data (used to upload files).

In your case, you do not need to specify the enctype which will already be the standard type (application/x-www-form-urlencoded):

<form action="Send2.php" method="post">

0

The tag action parameter <form> can only contain a relative URL action="meu-ficheiro-local.php" or an absolute URL action="http://www.exemplo.com/exemplo.php". In this case what seems to me is that the php statement is in the same file as <form>, but in turn the form sends its contents to the file Send2.php. <form action="Send2.php" method="post">

In my opinion the best thing to do in this case is to use the server variable $_SERVER["PHP_SELF"], which returns the name of the file currently running. We should use this global variable within the method htmlspecialchars() to avoid exploiting their vulnerabilities, below is an example of the implementation.

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >

Another issue is the use of a control structure, as this way the script will send two emails, one when started and the other when the form is submitted. To fix this we can use another server variable $_SERVER["REQUEST_METHOD"]which returns the type of method with which the file was requested, the two most used methods are the GET and the POST, below I leave an example of what would be the structure.

<?php

    //Só envia o email caso o tipo da requisição seja do tipo `POST`  
    if ($_SERVER["REQUEST_METHOD"] == "POST") {

      $name= $_POST['nome'];
      $email= $_POST['email'];

      echo $name;
      echo $email;

      $to = "[email protected]";
      $subject = "Contato Site - ManaSoft";
      $message = "Name: ".$name. "<br/> E-mail: ".$email. "<br/>";
      $header = "MIME-Version: 1.0\n";
      $header .= "Content-type: text/html; charset=iso-8859-1\n";
      $header .= "From: $email\n";

      mail($to, $subject, $message, $header);
    }
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.