![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
|
How to submit forms from one page to the other
Hello,
Does anyone know how to submit forms from one page to another page? If you don' mind can someone explain how, or if they know a good tutorial, either of these would be great. Thanks |
|
|
|
|
|
#2 |
|
Programmer
|
The "method" and "action" parameters of forms are used for that very purpose. "Action" is the page or script you want to send the from data to when the form is submitted. Valid values for method are "get" and "post." Setting to "get" appends all your data to the address of your target page after a question mark (?), using ampersands as delimiters (&). Like this:
(I put spaces in to stop the auto-parser from turning it into a link..) www. mySite .com/pageImSendingDataTo.html?inputName1=value1&inputName2=value2 [EDIT: You can also find an example at the top of this page! In terms of your form, t would be the name of an input in your form and 2167 its value.] "Post" on the other hand, transmits all your data but doesn't display it. (I know a name like "post" sound like that would be the one you'd see, but it isn't. Don't ask me why. I don't know. :p) The major difference between the two is that "get" data can be bookmarked, while "post" data is more secure. If you know php (and have it on your server), you can get at those values from the target page with the $_GET and $_POST global variables. (Check out www.php.net for more about that.) If you don't know php (or any other server-side languages) you can always use the value of location.search in JavaScript to get at "get" data. Unfortunately location.search holds everything after the question mark as one long string, so you'll have to parse it yourself. As for "post" data and JavaScript, I think you're outta luck. JavaScript either has direct access to "post" data using the form input names as variables, or it doesn't. I'm leaning toward the latter. Goolge around for get and post for more info, and happy form-writing. ![]()
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose? Last edited by EdSalamander; Feb 8th, 2005 at 10:23 PM. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
Thank ed, this was very helpful
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
say i have this in the body of my html file. What would be the corresponding code it the other html page that would retrieve the information? I know that you said that the you use the ? and &, but where do they come into play?
<form name="input" action="html_form_action.asp" method="get"> Username: <input type="text" name="user"> <input type="submit" value="Submit"> </form> Thanks |
|
|
|
|
|
#5 |
|
Programmer
|
Well, that's where PHP and/or JavaScript would come it. I would recomend using PHP by far (or another server-side language). The JavaScript method is much more convoluted and I'd try to steer clear of that. Using php though would go something like this..
(I'm gonna abstract it so that it'll apply to more general situations as well.) [php]<?php /*first, the simple matter of retrieving you data*/ $myVariable = $_GET['inputName']; /* -or if your using post- */ $myVariable = $_POST['inputName']; ?>[/php] In both of these cases, 'inputName' is the name of the input field from your form. it will also be what you see up top when using "get," like this: mySite. com/myDirectory/myFormHandler.php?inputName=myValue If you use "post" instead, then these fields will be hidden from view, but you'll have access to them in much the same way. Now, look back at my fake site address. Notice something odd? I used .php instead of .html for my file name. To explain why I did this will require that I explain php a bit. Exceuse me if I insult your intelligence along the way, I just want to make sure I cover all the bases. Now then, PHP, like all server-side scripting languages, are run on the host server before being sent to the client. The host, aka the server, is the computer that holds the webpage. The client is the computer that is asking the server if it can look at that webpage. Often, especially with HTML alone, the host just sends the page off to the client with out a second thought. With server-side scripts though, the host reads through the page first, then sends it off, but that file must end in .php (or the appropriate extension for whatever language you're using). If it doesn't end with .php, the server won't know to look through the file for coge that it can run. Whenever the host runs into scripting like PHP, it runs the script, makes the appropriate changes to the HTML page, then passes the HTML along minus the original PHP code. That way, you can have the page change what it does or looks like to the client depending on the results of the PHP. This allows for all sorts of neat features, like database interactivity, form handling, and code hiding (just to name a few). Pages like these are called dynamic pages, because they change depending on certain input or parameters. Back to your problem: In your case, PHP has the ability to examine get and post data and use it in the HTML before the HTML is even sent to the client. Just as I did above, you'll want to snatch out that data and stroe it in variables you can use. Then, you'll echo back the contents of the variables to the new HTML page. Like so: <input type="text" name="user" value="<?php echo $myVariable;?>"> <input type="text" name="user" value="myValue"> A few other notes: If you do decide to go the PHP route, you'll have to be using a server that has PHP installed. It's a very popular and very common language, but not everyone has access to it. And one last thing: keep in mind that this was a very quick and dirty explanation of PHP and form handling. I've left a lot of basic stuff out along the way, but hopefuly this will get you started. Besides, I'm sure (and hope) others from the forum will have more to add/contribute. ![]()
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose? |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
If you don't mind can you show me the javascript side. Thanks for the information of php too.
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
Ok, so I found a little on how to do it with javascript, but I think I am still missing elements. I found that the code on the recieving page needs
alert(opener.document.forms.formid['inputname'].value); Thanks |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
someone told me that if I was to write the code in javascript it would be good to make a function. Also they said that I should save it to a cookie, because I am not designing this for server side saving
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: Apr 2005
Location: Kansas
Posts: 22
Rep Power: 0
![]() |
if you use $_POST over $_GET it'll be more secure , so I've heard. Would anyone care to elucidate?
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() ![]() |
get puts the values into the query string which means that a cracker could get some of the form data and the varible names and create a form to add false data to what ever your form is doing.
Post doesn't send them through the query string, thus not allowing the cracker to see the varible names or the data. plus post makes your site look clean in reguards to the amount of jiberish in the address bar. Or at least that's why i think $_POST is more secure than $_GET. If anybody knows any other reasons why, please expand on my post.
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|