Send data from a form by email

Asked

Viewed 60 times

-1

My goal is to create a contact form, where the user entered his name, email and message. The moment you clicked on "send message" I, being an administrator, would receive an email, with the user’s data and the corresponding message. At the moment, the message sends, but sends empty for example:

Responda a:
Nome do Utilizador:
Email do Utilizador
Mensagem do Utilizador:

When sending the message there is also another problem that appears a new page, with the same form but without css, I do not know what is due

//Formulário contactos.php

<div class="col-md-7 mb-5 site-animate">
            <form action="index.php?cmd=contform" method="post">
              <div class="form-group">
                <label for="name" class="sr-only">Nome</label>
                <input type="text" class="form-control" id="NomeM" placeholder="Nome">
              </div>
              <div class="form-group">
                <label for="email" class="sr-only">Email</label>
                <input type="text" class="form-control" id="EmailMen" placeholder="Email">
              </div>
              <div class="form-group">
                <label for="message" class="sr-only">Mensagem</label>
                <textarea name="message" id="Mensagem" cols="30" rows="10" class="form-control" placeholder="Escreva a sua mensagem"></textarea>
              </div>
              <div class="form-group">
                <input type="submit" name="submit" class="btn btn-primary btn-lg" value="Enviar Mensagem">
              </div>
            </form>
          </div>
//Ficheiro contform.php

<?php
$sql="select * from Mensagem ";
$res=$lig->query($sql);
$lin = $res->fetch_array();


$NomeM = $_POST['NomeM'];
$Email = $_POST['EmailMen'];
$Mensagem = $_POST['Mensagem'];


$email_from = '[email protected]';

$email_subject = "New Form Submission";

$email_body = "Nome do Utilizador:" .$lin['NomeM']."\n".
"Email do Utilizador:".$lin['EmailMen']."\n".
"Mensagem do Utilizador:".$lin['Mensagem']."\n";


$to = "[email protected]";

$headers = "De: $email_from \r\n";

$headers .= "Responda a: ".$lin['EmailMen']."\r\n";

mail($to,$email_subject,$email_body,$headers);

header("Location: Contactos/contactos.php");

?>
  • And what is the value of your variable $lin? Stick to the verb is and not which should be.

  • The $Lin variable does not have a value, it is simply used to fetch data from my database

  • Someone, exactly. Is she searching the data correctly? What is the result of var_dump($lin)? What are the latest messages in your server’s error log and what are your PHP error display settings?

  • Does not return any error to me, nor does it display any error message

  • And what is the return of var_dump? About the errors, checked the log file or only on screen?

  • None, I simply checked on the screen, I have no log file

  • If var_dump You haven’t returned anything, there’s something very wrong there and maybe that’s why you’re sending it all blank. If the variable has no value, you have nothing to send in the email.

  • Take the opportunity to read how to properly configure your development environment to properly display error messages: https://answall.com/q/106562/5878

  • I noticed that you are using Mysqli to search for information in the database, in which case I suggest you see this tutorial if applicable. http://blog.thiagobelem.net/gui-pratico-de-mysqli-no-php You must create the query, then prepare it to run, as you are not passing parameters, you can skip the part of the bind_param, Next you need the execute and finally the fetch.

  • Thank you, I’ll try!

  • I have already analyzed the document and still do not know how to correct my mistake

Show 6 more comments

1 answer

0

Someone, you are saving the data that the user entered in the table Mensagem of the database?

If not its variable $email_body it should be like this:

$email_body = "Nome do Utilizador:" .$Nome."\n".
              "Email do Utilizador:".$Email."\n".
              "Mensagem do Utilizador:".$Mensagem."\n";

and these lines of code have no use at all:

$sql="select * from Mensagem ";
$res=$lig->query($sql);
$lin = $res->fetch_array();
  • Yes it had as objective to save the data entered in the form in the data group, thence to have made the second piece of code that inserted, so that it made a connection with the data group

  • Because if I use only the "$Name" "$Email" and "$Message" variables and do not make a connection to the database, the data will not be saved

  • I just tried the way you suggested and gave the same error, returns nothing

  • Okay, so let’s take a closer look, you’re getting the information from $_POST in the archive contform.php?

  • No, I don’t know why

Browser other questions tagged

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