-1

I have a problem with a contact form I'm building because it sends the message without sender info (name & email) and no message as well. And I need that. I tried the var_dump from another post I found but did not work.

HTML

<form action="form_process.php" class="contact_form" method= "post" onsubmit>
<ul>
 <li><label for="name">name:</label><input type="text" required /></li>
<li><label for="email">email:</label><input type="email" name="email" required /></li>
<li><label for="message">message:</label><textarea name="message" cols="40" rows="6" required ></textarea></li>
<li><button class="submit" type="submit">Enviar</button></li>
</ul>
</form>
</div>

PHP

<?php

$name = var_dump($_POST['name']);
$email = var_dump($_POST['email']);
$message = var_dump($_POST['message']);
$to = "mmechenique@gmail.com";
$subject = "Nuevo mensaje formulario de contacto";

mail ($to, $subject, $message, "From: " . $name);
echo "Tu mensaje ha sido enviado, muchas gracias!";

?>
Tiskolin
  • 167
  • 17
Margaret
  • 45
  • 9
  • 1
    `var_dump` returns void. – u_mulder Nov 25 '17 at 19:07
  • 1
    The var_dump() function is used to display structured information (type and value) about one or more variables. – Abhishek Nov 25 '17 at 19:08
  • 1
    Your `name` input doesn't actually have a `name` attribute, so that won't work. There are a lot of other weird things here, like an empty `onsubmit`. And your submit button should be `...`, not a `button` tag. Most importantly, you should have `$variable = $_POST['whatever'];`, without the `var_dump()`, as others have pointed out. – elixenide Nov 25 '17 at 19:08
  • @Margaret dont be a spammer host, or worse lose your hosting. [How to sanitize user input in PHP before mailing?](https://stackoverflow.com/questions/1055460/how-to-sanitze-user-input-in-php-before-mailing) – Lawrence Cherone Nov 25 '17 at 19:14

2 Answers2

2

Try using your form this way:

<form action="form_process.php" class="contact_form" method= "post">
<ul>
 <li><label for="name">name:</label><input type="text" name="name" required /></li>
<li><label for="email">email:</label><input type="email" name="email" required /></li>
<li><label for="message">message:</label><textarea name="message" cols="40" rows="6" required ></textarea></li>
<li><button class="submit" type="submit">Enviar</button></li>
</ul>
</form>
</div>

PHP:

<?php

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $to = "mmechenique@gmail.com";
    $subject = "Nuevo mensaje formulario de contacto";

    mail ($to, $subject, $message, "From: " . $name);
    echo "Tu mensaje ha sido enviado, muchas gracias!";

  ?>
Abhishek
  • 539
  • 5
  • 25
1

Your HTML should look like:

<form action="form_process.php" class="contact_form" method="post">
    <ul>
        <li>
            <label for="name">name:</label>
            <input type="text" name="name" required />
        </li>
        <li>
            <label for="email">email:</label>
            <input type="email" name="email" required />
        </li>
        <li>
            <label for="message">message:</label>
            <textarea name="message" cols="40" rows="6" required ></textarea>
        </li>
        <li>
            <button class="submit" type="submit">Enviar</button>
        </li>
    </ul>
</form>

Your PHP should look like:

<?php

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $to = "mmechenique@gmail.com";
    $subject = "Nuevo mensaje formulario de contacto";
    $headers  = "From: Enter something here < email@mail.com >\r\n";
    $headers .= "X-Sender: Enter something here < email@mail.com >\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();
    $headers .= "X-Priority: 1\r\n"; // Urgent message!
    $headers .= "Return-Path: email@mail.com\r\n"; // Return path for errors
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";

    mail ($to, $subject, $message, $headers);
    echo "Tu mensaje ha sido enviado, muchas gracias!";

?>

I shouldn't be giving out the answer so easily because you should have done your research before posting your question but what the hell. Now you know for next time to do SOME research before posting your question.

EDIT:

<?php
    $headers  = "From: Enter your name < myemail@mail.com >\n"; //If you are the one sending the email enter your name here
    OR
    $headers  = "From: ".$name." < ".$email." >\n"; // If you are the one the email is being sent to then try this header

    $headers .= "X-Sender: Enter your name < myemail@mail.com >\n"; //Same logic applies for this guy
    OR
    $headers .= "X-Sender: ".$name." < ".$email." >\n"; //Same logic applies for this guy
?>
JeanPaul98
  • 492
  • 6
  • 18
  • Mail headers *should* (RFC) use `\r\n` – Lawrence Cherone Nov 25 '17 at 19:21
  • well thanks for your incredible effort. Next time just skip beginners questions. I'm here trying to learn just how people do at their very beginnings. And I did research. – Margaret Nov 25 '17 at 19:26
  • @Margaret Probably wasn't easy for you to figure out since you're a beginner (everyone is TBH no matter how long they've been coding for) but did the answers we give you work? – JeanPaul98 Nov 25 '17 at 19:31
  • @LawrenceCherone thanks for reminding me, have updated answer. – JeanPaul98 Nov 25 '17 at 19:43
  • @JeanPaul98 looking much better now! I receive emails now but still can't see the name and original e-mail. It is something to do with "Enter something here"? – Margaret Nov 25 '17 at 19:46
  • @JeanPaul98 start working finally! one little question.. is this type of code form secure enough for spam or should I add a captcha or something? The website is just a portfolio. – Margaret Nov 25 '17 at 20:16
  • @Margaret definitely add captcha, it will make your life easier down the road and you won't have to deal with 50 spam emails from the website – JeanPaul98 Nov 25 '17 at 20:20