2015-03-19 60 views
0

我有一個要求輸入密碼的表單。如果輸入了正確的密碼,則會顯示另一個表單。如果沒有,則會出現警告框,然後重新加載頁面。現在我已經制作了一個php,以便沒有人能看到密碼。現在的情況是,我做的第二種形式是隱藏的,所以我做的是在隱藏和顯示錶單的按鈕中放置一個javascript function()。然後我在echo它在php中。現在的問題是,無論何時點擊提交按鈕,即使我沒有輸入密碼,表單仍然會顯示。我的問題是,我怎樣才能成功實現這個代碼並正確運行我的程序?onclick javascript in PHP

HTML

<!-- *** password form *** --> 
     <form id="frmPw" method="get" action="http://dashboard.site_practice.co/transfer/password_protect_transfer.php" style="float:left;"> 
     <label for="element_1"> 
     Enter password to view content. 
     </label> 
     <div> 
     <input id="pw" name="password" type="password" placeholder="" style="width:200px;" required=""/> 
     </div> 
     <button id="submit" name="submit" style="width: 100px; float:left;"> 
     Submit 
     </button> 
     </form> 

<!-- *** select form *** --> 
    <form method="POST" id="frmSelect" style="display:none;" action="http://dashboard.site_practice.co/transfer/transfer.php"> 
     <label class="control-label" for="selectbasic-0">Select an account :</label><br> 
     <select id="selectbasic-0" name="selectbasic-0" required=""> 
      <option value="1">ABC</option> 
      <option value="2">DEF</option> 
      <option value="3">GHI</option> 
      <option value="4">JKL</option> 
     </select> 
    <br> 
     <label class="control-label" for="name">Enter name:</label><br> 
     <input id="name" name="name" type="text" placeholder="Full Name" required="" style="width:150px;"> 
    <br> 
     <label class="control-label" for="submit"></label> 
     <button id="submit" name="submit" class="btn btn-success">Join!</button> 
    </form> 

JS

<script> 
function pW() { 
     document.getElementById('frmSelect').style.display = 'block'; 
     document.getElementById('frmPw').style.display = 'none'; 
} 
</script> 

PHP

<?php 

$pw = $_POST['password']; 

if($pw == "admin1234"){ 
    echo pW(); 
}else{ 
?> 
<script type="text/javascript"> 
    alert("Access Denied - Password Incorrect, Please Try Again"); 
</script> 
<?php 
} 
?> 
+0

你不能從php代碼中調用js函數 – 2015-03-19 02:26:52

+0

就像@Lashane說的,你不能做你正在做的事情。相反,對於第一條if語句,回聲如「成功」。請在頁面的JavaScript中使用if/else語句閱讀此內容,這就是您稱之爲「pW」功能的地方。 – jonmrich 2015-03-19 02:28:29

+0

所以你知道如何從PHP內部正確地調用一個JavaScript函數 - alert,但是你不能將這些知識轉移到調用另一個JS函數?順便說一句,你不需要首先使用JavaScript,而是隻需在表單上輸入'style =「display:none;」'屬性,通過檢查是否發送密碼在那裏,然後在PHP中。 – CBroe 2015-03-19 04:45:30

回答

1

我想你的代碼。它正在工作,當我將$_POST['password']更改爲$_GET['password']。我希望這會有所幫助。

<?php 

    $pw = $_GET['password']; //change $_POST to $_GET 
    if($pw == "admin1234"){ 
    echo pW(); 
    /////////////////////////////////////////////////////////////////// 
    ///if password is correct, here will be your code for next step//// 
    /////////////////////////////////////////////////////////////////// 
    }else{ 

?> 

<script type="text/javascript"> 
    alert("Access Denied - Password Incorrect, Please Try Again"); 
</script> 

<?php 
    } 
?>