HTML 5 模板中使用的 PHP 联系表单,但不发送电子邮件

我有一个 contact-form.php 和一个 index.html 文件,如下所示。但是当点击提交发送电子邮件时,它重新加载页面并没有发生任何事情,网站的主页再次刷新。我的问题在哪里?


我看到很多 SO 链接,但我找不到问题所在。我在这里附上了代码。所有这两个文件都在 PHP 服务器上的同一个文件夹中。


<!-- CONTACT FORM -->

                                <div class="">

                                    <form id="contact-form" action="contact-form.php" method="POST">


                                        <div class="row">

                                            <div class="col-md-12 mb-30">

                                                <!-- <label>Your name *</label> -->

                                                <input type="text" value="" data-msg-required="Please enter your name"

                                                    maxlength="100" class="controled" name="name" id="name"

                                                    placeholder="NAME" required>

                                            </div>

                                        </div>


                                        <div class="row">

                                            <div class="col-md-12 mb-30">

                                                <!-- <label>Your email address *</label> -->

                                                <input type="email" value=""

                                                    data-msg-required="Please enter your email address"

                                                    data-msg-email="Please enter a valid email address" maxlength="100"

                                                    class="controled" name="email" id="email" placeholder="EMAIL"

                                                    required>

                                            </div>

                                        </div>



慕神8447489
浏览 153回答 1
1回答

慕尼黑5688855

在 HTML 提交按钮上命名:<input&nbsp;type="submit"&nbsp;name="submit"&nbsp;value="SEND&nbsp;MESSAGE"&nbsp;class="button&nbsp;medium&nbsp;gray" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data-loading-text="Loading...">编辑:首先,把你的决赛if放在$_POST['submit']&nbsp;if.&nbsp;您只在提交时才这样做,并且$mail_status只在其中设置:使用$_GETparams,而不是#... 那些用于滚动到名称相同的锚点。将 index.html 转换为 index.php,以便您可以DIV使用 PHP控制要显示的内容。$result = $_GET['result']&nbsp;将在 URL 中的 = 之后包含任何内容&nbsp;?result=那么if($result == 'success') {&nbsp;?> <!-- Show Success Div --> <?php&nbsp;} else {&nbsp;?><!-- Show error DIV --> <?php这是您的 PHP,其中更改了重定向 URL 以使用$_GET参数并index.html转换为 PHP:<?phpif (isset($_POST['submit'])) {&nbsp; &nbsp; $to&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= "bbbbb@gmail.com";&nbsp; &nbsp; $from&nbsp; &nbsp; &nbsp; &nbsp;= $_POST['email'];&nbsp; &nbsp; $first_name = $_POST['name'];&nbsp; &nbsp; $subject&nbsp; &nbsp; = "Form submission";&nbsp; &nbsp; $message&nbsp; &nbsp; = $first_name . " " . " wrote the following:" . "\n\n" . $_POST['message'];&nbsp; &nbsp; $headers = "From:" . $from;&nbsp; &nbsp; $mail_status = mail($to, $subject, $message, $headers);&nbsp; &nbsp; if ($mail_status) {?>&nbsp; &nbsp;<script language="javascript" type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; window.location.href = 'index.php?result=success';&nbsp; &nbsp; </script><?php&nbsp; &nbsp; } else {?>&nbsp; &nbsp;<script language="javascript" type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location.href = 'index.php?result=failed';&nbsp; &nbsp; </script><?php&nbsp; &nbsp; }}?>
打开App,查看更多内容
随时随地看视频慕课网APP