tags:

views:

60

answers:

5

I'm trying to write some HTML 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!

A: 

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.

steven_desu
you dont need to use javascript. you can use php as well.
Pavan
Am I the only one who thinks that the OP wants to submit the form without refreshing the page, in which case he'd definitely need JS to submit the form through AJAX ?
FreekOne
@FreekOne: No ... it was one of three ways I suggested ...
GreenMatt
The OP said he didn't want the page to "refresh". That means that he doesn't want the browser to be send a new HTTP header. He wants AJAX.
steven_desu
@steven_desu: Actually the OP used the word "redirect", not "refresh". Maybe your interpretation is correct; if so, a JavaScript solution seems the only one. However, to me "redirect" simply means send the browser to a different page; thus there would be a variety of acceptable solutions, some using JavaScript (and thus possibly, but not necessarily AJAX) and some other means.
GreenMatt
A: 

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'&gt;
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.

sushil bharwani
A: 

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())
GreenMatt
Thats right. look at the post below.btw matt from goldsmiths?
Pavan
@Pavan: Please bear in mind that the sequence in which answers appear can be affected by voting, which answer (if any) is selected, and the fact that we don't all sort answers the same way. And, btw, probably not, unless you think you met me at the home of someone with the surname Goldsmith ...
GreenMatt
ok. I will definitely keep that in mind. Thank you for the heads up. And, its just that my friend who attends university also goes by the same name as yourself. :D i thought you were him lol
Pavan
A: 

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.

FreekOne
yep that's an ideal solution. This way nothing gets reloaded. This way is just brilliant. The ingenuity behind this. Reminds me of the way facebook works. update status and everything just gets shoved down :D brilliant i tell you brilliant lol
Pavan
jQuery = javascript. As was said several times, the only way to "not refresh the page" after submitting a form is using javascript =) Well, that or some client-side plugin like Flash.
steven_desu
A: 

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&amp;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

Pavan