【发布时间】:2015-02-06 22:31:40
【问题描述】:
每当用户提交表单时,我都会尝试显示()一个 div。整个网站是一页。目前你提交的时候,表单已经提交,然后.confirmation div出现了1秒左右就消失了,页面刷新,div消失了。
这是我的 HTML
<form id="contactform" class="col s12 l6" action="mailer.php" method="post" >
<div class="confirmation"><p>Thanks for contacting us. We will be in touch with you shortly.</p></div>
<div class="row">
<div class="input-field col s12">
<input name="inputName" type="text" class="validate">
<label for="inputName">Name</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input name="inputEmail" type="email" class="validate">
<label for="inputEmail">Email</label>
</div>
<div class="input-field col s6">
<input name="inputPhone" type="text" class="validate">
<label for="inputPhone">Phone</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<textarea class="materialize-textarea" name="inputMessage"></textarea>
<label>Message</label>
</div>
</div>
<div class="row">
<button id="submit-button" class="btn waves-effect waves-light cyan col s6" type="submit">Submit
<i class="mdi-content-send right"></i>
</button>
</div>
</form>
<script src="js/scripts.js"></script>
CSS
.confirmation {
display: none;
}
这是我的 PHP
<?php
/* Set e-mail recipient */
$myemail = "krm218@gmail.com";
/* Check all form inputs using check_input function */
$name = $_POST['inputName'];
$email = $_POST['inputEmail'];
$phone = $_POST['inputPhone'];
/* $inquiry = $_POST['inputInquiry']; */
$message = $_POST['inputMessage'];
/* Let's prepare the message for the e-mail */
$subject = "Cleaning Services Contact or Estimate";
$message = "
Victorias Cleaning Services Contact Form
Name: $name
Email: $email
Phone: $phone
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: index.html#contact');
exit();
?>
还有一个 scripts.js 文件
$('#submit-button').click(function(){
$(".confirmation").show();
});
提前致谢!!
【问题讨论】:
-
当您点击提交时,表单会将表单数据发送到 mailer.php,然后向用户显示该请求的结果(重新加载页面)因此在页面之前运行的任何 javascript/jquery重新加载无关紧要。在 PHP 中尝试这样的事情:
<div class="confirmation" style="<?php if(!isset($_POST)) echo 'display: none'; ?>">这样,你也不需要 css -
我把它放在了 php 表单上,但是 .confirmation div 现在显示在浏览器中,因为我关闭了 css。是否可以在开始时隐藏并在页面重新加载并提交表单后显示?
-
这就是我的例子应该发生的事情......
-
我发的php要放到你发的html最上面,替换确认div的开始标签..
-
所以我这样做了,victoriacleans.com/vc.com/index.html 联系部分有点坏了....在我将它放入 html 之前,推荐 div 是 100%..?
标签: javascript php forms submit