Automatically filling a form and submitting it doesn't get the parameters to the server
I have a form that is filled automatically by a JS
My F开发者_开发百科orm is:
<form id="doSubmit" method="post">
<input type="text" id="usr" />
<input type="password" id="pwd" />
</form>
My JS is:
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();
Now on the next page which is PHP, when I try to use the I get this error Undefined index
and when I type print_r($_POST)
I get Array()
Any idea how I can post the form data on the next page?
Your input
elements need to have a name
attribute:
<form id="doSubmit" method="post" action="">
<input type="text" id="usr" name="usr" />
<input type="password" id="pwd" name="pwd" />
</form>
After you've done this, you should be able to see the usr
and pwd
keys in the $_POST
array in your receiving PHP script. (The keys will be the HTML name
attribute and the value will be the corresponding value
HTML attribute.)
So, say your form ended up looking like this:
<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>
Your $_POST
array would look like this:
Array (
'usr' => 'someuser',
'pwd' => 'somepassword'
)
jsFiddle example
精彩评论