我有一个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>
<div class="row">
<div class="col-md-12 mb-40">
<!-- <label>Message *</label> -->
<textarea maxlength="5000" data-msg-required="Please enter your message"
rows="3" class="controled" name="message" id="message"
placeholder="MESSAGE" required></textarea>
</div>
</div>
<div class="row">
<div class="col-md-12 text-center-xxs">
<input type="submit" value="SEND MESSAGE" class="button medium gray"
data-loading-text="Loading...">
</div>
</div>
</form>
<div class="alert alert-success hidden animated fadeIn" id="contactSuccess">
<strong>Success!</strong> Your message has been sent to us.
</div>
<div class="alert alert-danger hidden animated shake" id="contactError">
<strong>Error!</strong> There was an error sending your message.
</div>
</div>
</div>
</div>
</div>
</div>
和
<?php
if(isset($_POST['submit'])){
$to = "bbbbb@gmail.com";
$from = $_POST['email'];
$first_name = $_POST['name'];
$subject = "Form submission";
$message = $first_name . " " . " wrote the following:" . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$mail_status= mail($to,$subject,$message,$headers);
}
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
window.location.href = 'index.html#contactSuccess';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
window.location.href = 'index.html#contactSuccess';
</script>
<?php
}
?>
答案 0 :(得分:3)
在HTML提交按钮上放置一个名称:
<input type="submit" name="submit" value="SEND MESSAGE" class="button medium gray"
data-loading-text="Loading...">
编辑:
首先,将最终的if
放在$_POST['submit']
if
内。您只能在提交时进行此操作,并且$mail_status
仅在其内部进行设置:
使用$_GET
参数,而不是#
...这些参数用于滚动到名称相同的锚点。
将index.html转换为index.php,以便您可以控制使用PHP显示的DIV
。
$result = $_GET['result']
将包含?result=
那么
if($result == 'success') {
?> <!-- Show Success Div --> <?php
} else {
?><!-- Show error DIV --> <?php
这是您的PHP,其中对重定向URL进行了更改,以使用$_GET
参数和index.html
转换为PHP:
<?php
if (isset($_POST['submit'])) {
$to = "bbbbb@gmail.com";
$from = $_POST['email'];
$first_name = $_POST['name'];
$subject = "Form submission";
$message = $first_name . " " . " wrote the following:" . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$mail_status = mail($to, $subject, $message, $headers);
if ($mail_status) {
?>
<script language="javascript" type="text/javascript">
window.location.href = 'index.php?result=success';
</script>
<?php
} else {
?>
<script language="javascript" type="text/javascript">
window.location.href = 'index.php?result=failed';
</script>
<?php
}
}
?>