PHP表单提交给自己不起作用


php form submit to itself not working

我有一个 html 表单,它自己提交给它,这里是 html 表单

<div class="col-md-6 contact-grid">
    <form name="submitted" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
        <div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
            <input type="text" id="name" required />
            <label>Name</label>
            <span></span> </div>
        <div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
            <input type="email" id="email" required />
            <label>Email</label>
            <span></span> </div>
        <div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
            <input type="tel" id="phone" required />
            <label>Phone</label>
            <span></span> </div>
        <div class="styled-input wide wow slideInUp animated animated" data-wow-delay=".5s">
            <textarea id="message" required></textarea>
            <label>Message</label>
            <span></span> </div>
        <div class="send wow shake animated animated" data-wow-delay=".5s">
            <input name="submit" type="submit" value="Send">
        </div>
    </form>
</div>

下面是我处理表单的 PHP 代码

    <?php
session_start();
//error_reporting(0);
if(isset($_POST['submitted'])) {    
    $sendto   = "info@davfubgroup.com";
    $usermail = strip_tags($_POST['email']);
    $content  = nl2br($_POST['message']);
    $phone = strip_tags($_POST['phone']);
    $name = strip_tags($_POST['name']);
    $subject  = "New Feedback Message";
    $headers  = "From: " . strip_tags($usermail) . "'r'n";
    $headers .= "Reply-To: ". strip_tags($usermail) . "'r'n";
    $headers .= "MIME-Version: 1.0'r'n";
    $headers .= "Content-Type: text/html;charset=utf-8 'r'n";
$msg  = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New User Feedback</h2>'r'n";
$msg .= "<p><strong>Sent by:</strong> ".$usermail."</p>'r'n";
$msg .= "<p><strong>Message:</strong> ".$content."</p>'r'n";
$msg .= "<p><strong>Name:</strong> ".$name."</p>'r'n";
$msg .= "<p><strong>Phone:</strong> ".$phone."</p>'r'n";
$msg .= "</body></html>";

if(mail($sendto, $subject, $msg, $headers)) {
    $_SESSION['errormsg'] = "Mail Sent. Thank you we will contact you shortly.";
    echo '<center><p style="color:green">' . $_SESSION['errormsg']. '</p></center>';
} else {
    $_SESSION['errormsg'] = "Mail not Sent. Try Again.";
    echo '<center><p style="color:red">' . $_SESSION['errormsg']. 
'</p></center>';
    }
}
?>

但我的问题是当提交表单时,没有显示任何消息,也没有错误消息,请提供任何帮助

只需将输入的名称更改为

 <input name="submitted" type="submit" value="Send">

它会工作

它不起作用,因为您询问$_POST是否有一个名为 submitted 的键,但没有具有该名称的输入元素。

相反,您可以通过询问以下问题来检查$_POST中是否有任何数据:

if (count($_POST) > 0)

您的帖子检查应该是:strtoupper($_SERVER['REQUEST_METHOD']) === 'POST'而不是:isset($_POST['submitted'])。也不要使用count($_POST)因为用户可以发布空表单(也许表单只是用户未选中的复选框(。

当您提交表单时,每个字段都将在$_POST中可用,但表单标签上的 name 属性不会提交,并且在您提交表单时被 PHP 完全忽略(不确定那个顺便说一句(。

在给定的示例中,$_POST将仅包含一个键submit因为这是唯一具有name属性的输入元素。

在您的代码中,假设您已提交表单,则$_POST['submitted']是未定义的,因此永远不会isset($_POST['submitted'])计算为true

您还需要验证 PHP 中的用户输入,required 属性不能以任何方式保证服务器端数据的有效性。您可以-至少-检查字段是否为空和/或提供的电子邮件是否对PHP的本机函数有效filter_var()

if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
    //   The provided email is invalid
};

您应该考虑使用<span style="text-align: center;"></span>而不是无效的<center>

希望对您有所帮助。

虽然已经回答了,但在 isset(( 中,您使用的是表单的名称,因为代码的其余部分可以在使用初始 isset(( 时处理数据的实际有用性,您应该使用分配给提交按钮的名称,在这种情况下,这将是if(isset($_POST['submit']){}然后输入其余代码,这是从错误中学习的方法之一,在这种情况下使用整个表单的名称将使您一事无成,请参阅表单的各个输入元素祝你好运,希望这有帮助