2017-06-17 80 views
0

我寫了一個彈出窗口註冊一個網站。它的工作正常,除了一個方面 - 當用戶未能提供所有必要的信息時,彈出窗口將消失,但隨後再出現錯誤消息。同樣,如果用戶提供所有信息和點擊發送,則該消息將成功發送,但彈出消失,然後再次顯示成功消息。當用戶點擊關閉時,彈出窗口關閉並保持不動。我不知道爲什麼彈出消失,當出現錯誤或當它發送時重新出現

我一直在檢查代碼,我只是無法找出它爲什麼會消失,然後再次出現。我希望它留在那裏並顯示錯誤消息或成功消息,然後在用戶單擊「關閉」時纔會消失。這裏是以下代碼 - HTML,CSS,JS和PHP

這裏是website,所以你可以看到它的行動。

我欣賞任何和所有的建議!由於

HTML

<?php include $_SERVER['DOCUMENT_ROOT']. '/php/signup-popup.php'; ?> 
     <div id="pop-wrapper"> 
    <!-- Popup Div Starts Here --> 
    <div id="popupContact"> 
    <!-- Contact Us Form --> 
       <form action="#" id="form" method="post" name="form"> 
     <h2 id="popup-h2">Join the Civil War Digital Digest</h2> 
     <hr> 
     <input id="name" name="name" placeholder="Name" type="text" value="<?php echo htmlspecialchars($_POST['name']);?>"> 
     <input id="email" name="email" placeholder="Email" type="text" value="<?php echo htmlspecialchars($_POST['email']);?>"> 
     <textarea id="msg" name="message" placeholder="Message"><?php echo htmlspecialchars($_POST['message']);?></textarea> 
        <?php echo "<p id='error-message' class='text-danger'>$errError</p>";?> 
        <?php echo $result; ?> 
        <button id="submit" name="submit" type="submit" value="Send">Send</button> 
     <br> 
     <button onclick="hide()" id="close">Close</button> 
       </form> 
    </div> 
    <!-- Popup Div Ends Here --> 
     </div> 

的Javascript

function show() { 
    if(sessionStorage.getItem("close") == null){ 
     document.getElementById('pop-wrapper').style.display = "block"; 
    } 
    else { 
     document.getElementById('pop-wrapper').style.display = "none"; 
    }   
} 

    function hide(){ 
     sessionStorage.setItem("close", 1); 
     document.getElementById('pop-wrapper').style.display = "none"; 
    } 


    window.onload=function(){ 
     setTimeout(function(){ 
      show(); 
     }, 500); 
    } 

PHP

CSS

/* popup header color #394650 */ 
     /*font IM Fell DW Pica */ 

     /*---------------------------------------------- 
     CSS settings for HTML div Exact Center 
     ------------------------------------------------*/ 
     #pop-wrapper { 
      width:100%; 
      height:100%; 
      opacity:.99; 
      top:0; 
      left:0; 
      display:none; 
      position:fixed; 
      background-color:#313131; 
      overflow:auto; 
     } 

      div #popupContact { 
       position:absolute; 
       left:50%; 
       top:17%; 
       margin-left:-202px; 
       font-family:'IM Fell DW Pica', serif; 
      } 

      form { 
           width: 450px; 
           height: 680px; 
       /*max-width:300px; 
       min-width:250px;*/ 
       padding:10px 50px; 
       border:2px solid gray; 
       border-radius:10px; 
       font-family:raleway; 
       background-color:#fff; 
      } 

      #error-message { 
       /*margin-top:30px;*/ 
           font-size: 18px !important; 
      } 

      #popup-h2 { 
       background-color:#394650; 
       padding:20px 35px; 
       margin:-10px -50px; 
       text-align:center; 
       border-radius:10px 10px 0 0; 
       color: #fff; 
      } 
      hr { 
       margin:10px -50px; 
       border:0; 
       border-top:1px solid #ccc; 
      } 
      input[type=text] { 
       width:82%; 
       padding:10px; 
       margin-top:30px; 
       border:1px solid #ccc; 
       padding-left:40px; 
       font-size:16px; 
       font-family: raleway; 
      } 
      #name { 
       background-repeat:no-repeat; 
       background-position:5px 7px; 
      } 
      #email { 
       background-repeat:no-repeat; 
       background-position:5px 7px; 
      } 
      textarea { 
       background-repeat:no-repeat; 
       background-position:5px 7px; 
       width:82%; 
       height:95px; 
       padding:10px; 
       resize:none; 
       margin-top:30px; 
       border:1px solid #ccc; 
       padding-left:40px; 
       font-size:16px; 
       font-family:raleway; 
       margin-bottom:30px; 
      } 
      #submit, #close { 
       text-decoration:none; 
       width:100%; 
       text-align:center; 
       display:block; 
       background-color:#394650; 
       color:#fff; 
       border:1px solid #fff; 
       padding:10px 0; 
       font-size:20px; 
       cursor:pointer; 
       border-radius:5px; 
      } 
      span { 
       color:red; 
       font-weight:700; 
      } 
      button { 
       width:10%; 
       height:45px; 
       border-radius:3px; 
       background-color:#cd853f; 
       color:#fff; 
       font-family:'IM Fell DW Pica', serif; 
       font-size:18px; 
       cursor:pointer; 
      } 
+0

當您單擊彈出式窗口中的按鈕時,您將發送請求回到您的服務器,因此整個頁面會重新加載。如果您不希望重新加載頁面,則需要使用Ajax。 – csmckelvey

回答

0

您的表單的提交請求基本上重新加載頁面,因此再次啓動window.onload功能

相關問題