开发者

Php form not working

So I have this PHP form with basic name, email and message inputs, however I can't get it to work! I've tried putting ID and NAME tags for each of my form elements but it won't work.

Here's the php snippet

<?php
if(isset($_POST['submit'])) {

    $to = "xxx@example.com.br"; 
    $subject = "Success!";
    $name_field = $_POST['nome'];
    $email_field = $_POST['email'];
    $message = $_POST['mensagem'];
    $body = "From: $name_field\n E-Mail: $email_field\n $check_msg\n Message:\n $message\n";
    echo "Your form was sent!";
    mail($to, $su开发者_如何学运维bject, $body);

} else {
    echo "Ouch.";
}
?>

and the form

<form action="../php/send.php" method="post">
<label>Name:</label><input id="name" name="name" /><br />
<label>Email:</label><input id="email" name="email" /><br />
<label>Message:</label>
<textarea id="message" name="message" ></textarea><br />

<button type="submit" id="submit"></button>
</form>


You haven't defined a 'submit' field (there's no name="submit" in the form), so your if() test is failing.

A better method to check for POST is

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   ...
}

That entry is always present, and is far more reliable than checking for a particular field.


I suggest you to replace this code:

if(isset($_POST['submit'])) {

with something obvious, like:

if(isset($_POST['email'])) {

Also, there are couple of mistakes:
This:

$name_field = $_POST['nome'];
$email_field = $_POST['email'];
$message = $_POST['mensagem'];

Should be:

$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];

Also, you need to check that mail() returns true. Change:

echo "Your form was sent!";
mail($to, $subject, $body);

To:

if(mail($to, $subject, $body)) {
    echo "Your form was sent!";
} else {
    echo "mail() returned error! Your form was not sent!";
}

If you are sure that mail() is actually being executed without any errors, but mails are still not being sent out / reached the recipient, check the server logs, perhaps you need to configure php (normally not) or sendmail.


Instead of $name_field = $_POST['name']; within the POST checking statement, use $name_field = $_REQUEST['name']; The $_REQUEST will always grab the name= of an input field of the form you submitted from.

<?php
if(isset($_POST['submit'])) {

    $to = "xxx@xxx.com.br"; 
    $subject = "Success!";
    $name_field = $_REQUEST['name'];
    $email_field = $_REQUEST['email'];
    $message = $_REQUEST['message'];
    $body = "From: $name_field\n E-Mail: $email_field\n $check_msg\n Message:\n $message\n";
    echo "Your form was sent!";
    mail($to, $subject, $body);
    // now do something else like adding thank you 

} else {
    echo <<< END
<form action="../php/send.php" method="post">
<label>Name:</label><input id="name" name="name" /><br />
<label>Email:</label><input id="email" name="email" /><br />
<label>Message:</label>
<textarea id="message" name="message" ></textarea><br />

<button type="submit" id="submit"></button>
</form>

END;
}

?>

That should work


It is just a DOM error just change your input field name="myname" or anything else it'll solve your problem.

<form action="../php/send.php" method="post">
<label>Name:</label><input id="name" name="my_name" /><br />
<label>Email:</label><input id="email" name="email" /><br />
<label>Message:</label>
<textarea id="message" name="message" ></textarea><br />

<button type="submit" id="submit"></button>
</form>


There is no name attribute attached to your submit button, the PHP code you have written listens for POST data name "submit".
Change this:

<form action="../php/send.php" method="post">
<label>Name:</label><input id="name" name="name" /><br />
<label>Email:</label><input id="email" name="email" /><br />
<label>Message:</label>
<textarea id="message" name="message" ></textarea><br />

<button type="submit" id="submit"></button> <!-- Add a name attribute -->
</form>

To this:

<form action="../php/send.php" method="post">
<label>Name:</label><input id="name" name="name" /><br />
<label>Email:</label><input id="email" name="email" /><br />
<label>Message:</label>
<textarea id="message" name="message" ></textarea><br />

<button type="submit" id="submit" name="submit"></button> <!-- Name attribute added -->
</form>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜