单击提交后,表格发送邮件操作不起作用

时间:2019-01-05 08:22:03

标签: php html

我在我的索引页面上有一个html表单,在填充了字段并单击了提交按钮之后,表单动作移到了一个新页面,我名为“ indexform.php”,但未在“ indexform.php”页面。

以下是我的html表单代码:

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

<input name="name" type="text" class="form-control" placeholder="Your Name">

<input name="email" type="text" class="form-control" placeholder="Your Email">

<input name="subject" type="text" class="form-control" placeholder="Subject">

<input name="budget" type="text" class="form-control" placeholder="Your Budget">

<textarea name="message" id="" cols="30" rows="7" class="form-control" placeholder="What kind of content marketing services are you looking for?"></textarea>

<input type="submit" formaction="indexform.php" name="submit" value="Send Message">

</form>

下面是我在indexform.php页面上的php代码:

<?php

$mail_to_send_to = "joseph@digitage.net";
$from_email = "joseph@digitage.net";
$sendflag = $_REQUEST['sendflag'];
if ($sendflag == "send") {
    $email = $_REQUEST['email'];
    $message = $_REQUEST['message'];
    $name = $_REQUEST['name'];
    $budget = $_REQUEST['budget'];
    $headers = "From: $from_email" . "\r\n" . "Subject: $subject" . "\r\n" .
            "Reply-To: $email" . "\r\n";
    $a = mail($mail_to_send_to, "Message from a Home page contact form", $message, $headers);
    if ($a) {
        print("Message was sent, you can send another one");
    } else {
        print("Message wasn't sent, please check that you have changed emails in 
the bottom");
    }
}
?>

执行表单操作后,我没有收到任何消息。

2 个答案:

答案 0 :(得分:0)

您在按钮元素中不需要formaction="index.php"属性。只要您在action="indexform.php"标记中包含<form>,它将向该URL发送POST请求。

<form method="post" action="indexform.php">
    <input name="name" type="text" class="form-control" placeholder="Your Name" />
    <input name="email" type="text" class="form-control" placeholder="Your Email" />
    <input name="subject" type="text" class="form-control" placeholder="Subject" />
    <input name="budget" type="text" class="form-control" placeholder="Your Budget" />
    <textarea name="message" cols="30" rows="7" class="form-control" placeholder="What kind of content marketing services are you looking for?"></textarea>
    <input type="submit" name="submit" value="Send Message" />
</form>

我在您的PHP中注意到,您正在检查输入字段'sendflag',但是HTML中没有输入指令,因此实际上每次您检查“ sendflag”时,它始终为NULL。另外,您实际上应该使用$_POST而不是$_REQUEST来获取提交的输入字段。之所以称为“ POST”,是因为您要向指定的URL发送HTTP POST请求-您通过method="post"属性选择了“ POST”。

尝试以下方法:

<?php

$mail_to_send_to = "joseph@digitage.net";
$from_email = "joseph@digitage.net";

if (!empty($_POST)) {
    $email = $_POST['email'];
    $message = $_POST['message'];
    $name = $_POST['name'];
    $budget = $_POST['budget'];
    $headers = "From: $from_email" . "\r\n" . "Subject: $subject" . "\r\n" .
        "Reply-To: $email" . "\r\n";
    $a = mail($mail_to_send_to, "Message from a Home page contact form", $message, $headers);
    if ($a) {
        echo "<p>Message was sent, you can send another one</p>";
    } else {
        echo "<p>Message wasn't sent, please check that you have changed emails in the bottom</p>";
    }
}
?>

检查是否已提交任何表单的一种好方法是使用if (!empty($_POST))empty() function

如果愿意,可以使用method="get"编写表单,然后所有输入字段都将显示在URL中,如下所示:

indexform.php?name=my+name&email=me%40example.com&subject=My+Subject    ... etc.

然后,您当然可以使用$_GET数组来检索它们。我知道这可能不是您想要的,我只是在解释两个主要HTTP请求之间的区别。

如果需要,您还可以使用<button>元素作为表单提交者,它将与<input type="submit">相同。

<button>Send Message</button>

答案 1 :(得分:-1)

通过$_POST超全局变量而不是通过$_REQUEST访问POST变量,将$_REQUEST更改为$_POST,这应该可以解决问题。

相关问题