[英]Loading a thank you pop up div on form submit
我在提交時加載jQuery模態窗口時遇到問題。
我希望div在提交時加載,以感謝用戶參加比賽。 如果我添加如下所示的href,則div可以很好地打開: <a href="#dialog" name="modal">click here"</a>
但是,一旦提交,我似乎無法制作此彈出窗口。 難道我做錯了什么!?
這是我的形式php代碼:
<?php
if($_POST['formSubmit'] == "Submit")
{
$errorMessage = "";
if(empty($_POST['formName']))
{
$errorMessage .= "<li>You forgot to enter your name</li>";
}
if(empty($_POST['formTown']))
{
$errorMessage .= "<li>You forgot to enter your town</li>";
}
$varName = $_POST['formName'];
$varTown = $_POST['formTown'];
$varAge = $_POST['formAge'];
$varEmail = $_POST['formEmail'];
$varOne = $_POST['hidden-one'];
$varTwo = $_POST['hidden-two'];
$varThree = $_POST['hidden-three'];
$varFour = $_POST['hidden-four'];
$varFive = $_POST['hidden-five'];
if(empty($errorMessage))
{
$fs = fopen("mydata.csv","a");
fwrite($fs,$varName . ", " . $varTown . ", " . $varAge . ", " . $varEmail . ", " . $varOne . $varTwo . $varThree . $varFour . $varFive . "\n");
fclose($fs);
header("Location: #dialog");
exit;
}
}
?>
這是jQuery代碼:
<script>
$(document).ready(function() {
//select all the a tag with name equal to modal
$('a[name=modal]').click(function(e) {
//Cancel the link behavior
e.preventDefault();
//Get the A tag
var id = $(this).attr('href');
//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();
//Set heigth and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});
//transition effect
$('#mask').fadeIn(1000);
$('#mask').fadeTo("slow",0.8);
//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();
//Set the popup window to center
$(id).css('top', winH/2-$(id).height()/2);
$(id).css('left', winW/2-$(id).width()/2);
//transition effect
$(id).fadeIn(2000);
});
//if close button is clicked
$('.window .close').click(function (e) {
//Cancel the link behavior
e.preventDefault();
$('#mask').hide();
$('.window').hide();
});
//if mask is clicked
$('#mask').click(function () {
$(this).hide();
$('.window').hide();
});
});
</script>
這是我的html:
<form action="enter.php" method="post" target="_self">
<input class="submit" type="submit" name="formSubmit" value="Submit">
</form>
<div id="boxes">
<div id="dialog" class="window">
<p>Modal content here</p>
</div>
</div>
</div>
</div>
<!-- Mask to cover the whole screen -->
<div id="mask"></div>
</div>
如果您有一個“提交”按鈕,並且不調用preventdefault或類似的內容,則表單會被發布,頁面也會刷新。 因此,您的js代碼不再執行。 您需要做的是使用普通按鈕或鏈接並將onclick事件綁定到該事件,該事件首先顯示“謝謝”彈出窗口,然后“手動”通過JavaScript提交表單。
聽起來您可能對Web瀏覽器和Web服務器之間發生的事情有些困惑。
您的網絡瀏覽器正在運行一個呈現HTML / CSS並執行JavaScript的客戶端。 您的瀏覽器將向Web服務器提交請求,該服務器執行PHP以生成響應。
提交表單后,將請求發送到Web服務器,該服務器會生成響應-通常是另一個HTML頁面。 這是您應該發送感謝信息的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.