来自PHP的联系不起作用

时间:2016-07-29 07:27:27

标签: php html forms

这里的问题是什么?为什么它不起作用。在HTML页面按钮发送到这里。每当我尝试发送,总是工作其他部分。

<?php

if(isset($_POST['submit'])) 
{
    $to="mail@mail.com";
    $subject="Form to email message";
    $name=$_POST["user_name"];
    $mail=$_POST["user_mail"];
    $message=$_POST["user_message"];

    $mailBody="Name: $name\nEmail: $mail\n\n$message";

    mail($to, $subject, $mailBody, "From: $name <$mail>");
    echo "Data has been submitted to $to!";

}
else{
        echo "Upps!Something happened.";
    }

?>

这是html页面。我尝试更改按钮的名称。但根本没有工作。

mainpage.html

<html>
<head>
    <title>Test</title>
    <link rel="stylesheet" type="text/css" href="mainpage.css">


</head>
<body>

<h2><b><center>About Me</center></b></h2>

<form action="send.php" method="post">
    <div>
        <label for="name">Name:</label>
        <input type="text" id="name" name="user_name" >
    </div>
    <div>
        <label for="mail">E-mail:</label>
        <input type="email" id="mail" name="user_mail">
    </div>
    <div>
        <label for="msg">Message:</label>
        <textarea id="msg" name="user_message"></textarea>
    </div>
    <div class="button">
        <input type="submit" value="Send your message">

    </div>

</form>

</body>
</html>

谢谢

3 个答案:

答案 0 :(得分:1)

编辑:

鉴于你还没有提供任何HTML(我),我为你做了一个标准的标准:

您的表单需要如下所示:

<form action="" method="post">
    Username: <input type="text" name="user_name">
    Email: <input type="text" name="user_mail">
    <textarea name="user_message" id="" cols="30" rows="10"></textarea>
    <input type="submit"name="submit">
</form>

PHP将是(稍作调整后):

<?php

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["user_name"]));
    $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["user_mail"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["user_message"]);

    // Check that data was sent to the mailer.
    if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Please ensure you fill in all of the fields!";
        exit;
    }


    // admins email address
    $recipient = "mail@mail.com";

    // Set the email subject.
    $subject = "Enquiry Received From $name";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Message:\n$message\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Thank You! Your message has been sent.
            ";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }

}

?>

答案 1 :(得分:1)

原因可能很多,让我把它们中的一些:

  1. 第一件事主要是 $ _ POST [&#39;提交&#39;] 未设置

  2. HTML文件中的提交按钮可能具有不同的名称,意思是:

  3.   

    它应该与下面完全相同。试试这个并提交它可能有用:

    <input type="submit" name="submit" value="Submit" />
    
    1. 您提交的表单可能有标记错误。
    2. 正如其他人所说,请分享html。很容易识别问题。

答案 2 :(得分:0)

抱歉,我没有足够的代表发表评论,所以必须发回答..

正如其他人所说,html表单中的提交按钮需要名称(name =“submit”)。

我还会考虑在其他字段上进行验证,并在send.php文件中清理输入,例如:

<?php

$to="mail@mail.com";
$subject="Form to email message";

$name= filter_input(INPUT_POST, "user_name", FILTER_SANITIZE_TEXT);
$message= filter_input(INPUT_POST, "user_message", FILTER_SANITIZE_TEXT);
$mail= filter_input(INPUT_POST, "user_mail", FILTER_SANITIZE_EMAIL);


if( ! ($name === false || $message === false || $mail === false)
{
    $mailBody="Name: $name\nEmail: $mail\n\n$message";

    mail($to, $subject, $mailBody, "From: $name <$mail>");
    echo "Data has been submitted to $to!";
}
else
{
    echo "Upps!Something happened.";
}

这可能会更好,因为您获得了安全性的输入验证,但它也阻止了用户提交空白表单。