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 Form 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?

Answers


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


Need Your Help

CDN_SELCHANGE notification message generated by the FileOpenDialog work for 32bit build not for 64bit build

c# winapi pinvoke 32bit-64bit

I download "Extensible Dialogs Source"(C# using P/Invoke) from website which is written by Microsoft to show how to place Windows Forms controls inside one of the common file dialogs. (Like: add pr...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.