开发者

HTML: Update page vs. Redirecting to a New Page After User Input

I'm trying to write some HT开发者_如何转开发ML code so that a table will be created on the current page after the user submits inputs into a form. That is, instead of entering in values into a form, clicking submit, and being redirected to a new page with the computed output values, I'd like for the output values to appear at the bottom of the current page. This way, the user inputs stay intact and the only change is that an output table is appended to the bottom of the current page. Here's an example of how I would ideally like the page to work:

http://amort.appspot.com/

Any help would be very much appreciated. Thanks!


You need your form action to point to same page. lets say the URL of your page is

http://example.com/page.php than you need to point your action to same page.

<form action='http://example.com/page.php'>
and have inside your form a hidden field say
<input type='hidden' name='formSubmitted' value='1' >
when this form is submitted to itself you can check if hidden field has a value through the get parameters.

Once you get that value you can make a condition check to show up a table. But ofcoursr you will need to use a server side language like jsp or php for that.

One alternate way is you do not submit your form. But instead have a javascript called when you click submit query button. This javascript will read the values from the filled in form boxes and will display them in the table below.


You can use PHP in your HTML file.

Example simply create you form like so.

<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

When the user clicks the "Submit" button, the URL sent to the server could look something like this: http://www.w3schools.com/welcome.php?fname=Peter&age=37

The "welcome.php" file can now use the $_GET function to collect form data (the names of the form fields will automatically be the keys in the $_GET array): Then create a table and add the lines are the right places. so this would be your table for example and what you would write in the HTML file its as simple as that.

Welcome <?php echo $_GET["fname"];?>.<br />
You are <?php echo $_GET["age"]; ?> years old!

Hope that helps. Let me know if it does. Thanks

PK


You need to use javascript to do this. If you provided the current code of your page and the specific goal you wish to accomplish then I could give an example script, however if you feel comfortable coding it yourself then just look up javascript tutorials.


You could easily do this with jQuery. Submit the form to the calculator page with .ajax(), and have the calculator output its results as JSON. You can then output that result in the bottom table without the user ever leaving/reloading the page.

Hope this helps.


You don't say what you're using to process the form, but you won't be able to process it with just HTML. So you'll need some sort of processing with another language.

As others have mentioned, you could do this with JavaScript. AJAX would be one way, but you could write your own code to do this if you really want to (but I wouldn't recommend it if you don't have to).

Another way to do this is with PHP. Have the page process itself when the form is submitted.

Similar to the PHP suggestion, you could do this via a CGI script/program. The action specified within the form would be to call the page itself. Here is a simple Python example, assuming the name of your script is so_self_call.py:

#!/usr/bin/env python

import cgi
import sys

def end_page():
    print "</body>"
    print "</html>"

def print_form():
    print "<form action=\"so_self_call.py\">"
    print "<input type=\"text\" name=\"theText\">"
    print "<input type=\"submit\">"
    print "</form>"
    return

def start_page():
    print "Content-Type: text/html"
    print
    print "<html>"
    print "<head>"
    print "<title>Example of Python CGI script which calls itself</title>"
    print "</head>"
    print "<body>"
    return

def main():
    start_page()
    print_form()
    the_form = cgi.FieldStorage()
    if "theText" in the_form:
        print "From last time, theText = %s" % (the_form["theText"].value)
    end_page()
    return

if __name__ == "__main__":
    sys.exit(main())
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜