我有一個php文件,該文件可檢索以html形式輸入的信息并將其保存到mysql數據庫中。我面臨的問題是我無法弄清楚如何阻止頁面重定向到insertinfo.php。我希望用戶停留在同一index.html頁面上,在表單提交時將出現模式。以下是我的index.html和insertinfo.phpindex.html文件:<div class="form "> <form class="container" action="insertinfo.php"> <input type="text" id="firstname" class="form_input" placeholder="First Name" name="firstname" required> <input type="text" id="lastname" class="form_input" placeholder="Last Name" name="lastname" required> <input type="text" id="email" class="form_input" placeholder="Email Address" name="email" required> <input type="submit" id="myBtn" class="submit" value="Download eBook"> </form></div><div id="myModal" class="modal"> <!-- Modal content --> <div class="modal-content"> <div class="modal-header"> <div class="icon-box"> <i class="material-icons">check</i> </div> </div> <div class="modal-body"> <h4>An email with a download link towards the eBook has been sent to you.</h4> <p>Please check your inbox and your spam/bulk messages.</p> <a href="">Continue</a> </div> </div> </div><script type="text/javascript"> var modal = document.getElementById("myModal"); // Get the button that opens the modal var btn = document.getElementById("myBtn"); // When the user clicks the button, open the modal btn.onclick = function(event) { var fnameInput = document.getElementById("firstname").value; var lnameInput = document.getElementById("lastname").value; var emailInput = document.getElementById("email").value; if(fnameInput.length == 0 || lnameInput.length == 0 || emailInput.length == 0){ event.preventDefault(); alert("You must complete all existing fields"); } else { modal.style.display = "block"; } }</script>
2 回答

料青山看我應如是
TA貢獻1772條經驗 獲得超8個贊
該action
表格屬性描述的是<form>
將被提交。如果刪除它,該表單將提交到當前頁面。
因此,刪除此部分:action="insertinfo.php"
將阻止<form>
將您重定向到insertinfo.php
如果您仍然希望insertinfo.php
不重新加載就提交數據,請使用ajax庫或查看:如何使用XMLHttpRequest。
- 2 回答
- 0 關注
- 234 瀏覽
添加回答
舉報
0/150
提交
取消