开发者

how to get return value from server after POSTing from a html form

I h开发者_运维技巧ave a form :

<FORM name="loginForm" ACTION="https://www.example.com/cgi-bin/login.pl" METHOD="post">

The form submits a username and password to my perl script on the server which verifies the password is correct. However since my page login.html is a static page is it possible to have the Post return the value to me somehow in a javascript that will display to the user an error message if the password did not match for example, without having to go to a different page.

This can obviously be done by the server, serving a new page, but that's not what im looking for. Any ideas?


You want to use XMLHttpRequest to post the form contents from JavaScript.

If you're using jQuery, there is an example on the jQuery.post() page:

$.post("test.php", $("#testform").serialize()); // Assumes form id is "testform"

See the bottom example on the page ("Example: Post a form using ajax and put results in a div") to get more complete code for how to get the return value form the server.


There're many solutions for your problem. A simple one is in case of login failure let your login.pl returns an html containing a "redirect to login.html" element and with parameters indicating the error, the html looks like:

<META HTTP-EQUIV="Refresh" CONTENT="1; URL=login.html">

Your login.html should contains logic (in Javascript) to check parameter for error and display it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜