簡體   English   中英

自動填寫表單並將其提交不會將參數傳遞給服務器

[英]Automatically filling a form and submitting it doesn't get the parameters to the server

我有一個由JS自動填寫的表格

我的表格是:

<form id="doSubmit" method="post">
    <input type="text" id="usr" />
    <input type="password" id="pwd" />
</form>

我的JS是:

document.forms["doSubmit"].elements["usr"].value = usr;
document.forms["doSubmit"].elements["pwd"].value = psw;
document.forms["doSubmit"].action = location.pathname + "user";
document.forms["doSubmit"].submit();

現在在PHP的下一頁上,當我嘗試使用時,出現此錯誤Undefined index ,當我鍵入print_r($_POST)我得到Array()

知道如何在下一頁上發布表單數據嗎?

您的input元素必須具有name屬性:

<form id="doSubmit" method="post" action="">
    <input type="text" id="usr" name="usr" />
    <input type="password" id="pwd" name="pwd" />
</form>

完成此操作后,您應該可以在接收的PHP腳本的$_POST數組中看到usrpwd鍵。 (鍵將是HTML name屬性,而值將是相應的value HTML屬性。)

因此,假設您的表單最終看起來像這樣:

<form id="doSubmit" method="post" action="">
    <input type="text" id="usr" name="usr" value="someuser"/>
    <input type="password" id="pwd" name="pwd" value="somepassword"/>
</form>

您的$_POST數組如下所示:

Array (
  'usr' => 'someuser',
  'pwd' => 'somepassword'
)

jsFiddle示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM