开发者

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

开发者 https://www.devze.com 2023-01-27 22:13 出处:网络
I have a form that is filled automatically by a JS My F开发者_开发百科orm is: <form id=\"doSubmit\" method=\"post\">

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

0

精彩评论

暂无评论...
验证码 换一张
取 消