表单弹出框错误

时间:2018-03-31 19:51:24

标签: javascript php html forms popup

我有一个PHP表单,其动作链接是另一个PHP页面,它将用户输入的数据添加到数据库中,然后将它们重定向到另一个页面。

我正在尝试在将PHP表单发送到其操作页面之前显示弹出窗口。我面临的问题是,无论何时按下提交/创建按钮,弹出窗口都会显示一秒钟,然后自动消失。

通过故障排除,我发现问题出现在表单标签中,因为当按钮超出其完美表现的形式时。

我的代码:

<?php
session_start();
?>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
#dialogoverlay{
    display: none;
    opacity: .8;
    position: fixed;
    top: 0px;
    left: 0px;
    background: #FFF;
    width: 100%;
    z-index: 10;
}
#dialogbox{
    display: none;
    position: fixed;
    background: #000;
    border-radius:7px; 
    width:550px;
    z-index: 10;
}
#dialogbox > div{ background:#FFF; margin:8px; }
#dialogbox > div > #dialogboxhead{ background: #666; font-size:19px; 
padding:10px; color:#CCC; }
#dialogbox > div > #dialogboxbody{ background:#333; padding:20px; 
color:#FFF; }
#dialogbox > div > #dialogboxfoot{  }
#dialogbox > div > #dialogboxfoot2{ }
</style>
<script>
function CustomAlert(){
this.render = function(dialog){
    var winW = window.innerWidth;
    var winH = window.innerHeight;
    var dialogoverlay = document.getElementById('dialogoverlay');
    var dialogbox = document.getElementById('dialogbox');
    dialogoverlay.style.display = "block";
    dialogoverlay.style.height = winH+"px";
    dialogbox.style.left = (winW/2) - (550 * .5)+"px";
    dialogbox.style.top = "100px";
    dialogbox.style.display = "block";
    document.getElementById('dialogboxhead').innerHTML = "Are you sure?";
    document.getElementById('dialogboxbody').innerHTML = dialog;
    document.getElementById('dialogboxfoot').innerHTML = '<button 
onclick="Alert.ok()">Cancel</button>';
    document.getElementById('dialogboxfoot2').innerHTML = '<button 
  onclick="Alert.ok()">Continue</button>';
}
this.ok = function(){
    document.getElementById('dialogbox').style.display = "none";
    document.getElementById('dialogoverlay').style.display = "none";
}
}
var Alert = new CustomAlert();
</script>
<title></title>
 <link rel="stylesheet" href="./css/form.css">
 <link rel="stylesheet" href="./css/style.css">
</head>
<body>
<div id="dialogoverlay"></div>
<div id="dialogbox">
<div>
<div id="dialogboxhead"></div>
<div id="dialogboxbody"></div>
<div id="dialogboxfoot"></div>
<div id="dialogboxfoot2"></div>
</div>
</div>
<header>
<nav>
    <div class="main-wrapper">
    <div id="branding">
    <!--  <li><h1><span><a href="index.php">ProjectNet</a></span></li> -->
    <h1>ProjectNet</h1>
    </div>
    <nav>
      <ul>
        <li class="current"><a href="homepage.php">Home</a></li>
        <li><a href="">Profile</a></li>
        <li><a href="">Members</a></li>
        <li><a href="">Inbox</a></li>
        <li><a href="about1.php">About</a></li>
      </ul>
    </nav>
</nav>
</header>

<section id="showcase1">
<div class="container">  
<form id="contact" action="includes/form_process.php" method="POST">
<h3>Creating a Group</h3>
<h4>Please fill out the sections below.</h4>
<fieldset>
  <input placeholder="Project title" type="text" name="name">
</fieldset>
<fieldset>
  <textarea placeholder="Description of the project...." type="text" 
name="message" ></textarea>
</fieldset>
<fieldset>
<button onclick="Alert.render('You are about to create a new group.')" 
name="submit" type="submit">Create</button>
</fieldset>
</form>
</div>
</section>
</body> 
</html>

1 个答案:

答案 0 :(得分:1)

似乎你想&#34;暂停&#34;提交直到用户确认或取消他的请求?很像肯定()会做什么?

然后它比这更复杂,你必须&#34;取消&#34;提交首先返回&#34; false&#34; (比如onclick =&#34; Alert.render(&#39;您即将创建一个新组。&#39;);返回false&#34;或者通过调用event.preventDefault()(并将事件添加到你的参数)

然后你需要确定你的&#34;确认&#34;按钮所以它做了一些事情: 通过重新提交表单(这次让它去) 或者通过重定向或任何其他方法。