Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 8th, 2005, 8:05 PM   #1
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
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
dark_omen is offline   Reply With Quote
Old Feb 8th, 2005, 9:15 PM   #2
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
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 9:23 PM.
EdSalamander is offline   Reply With Quote
Old Feb 9th, 2005, 3:27 PM   #3
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
Thank ed, this was very helpful
dark_omen is offline   Reply With Quote
Old Feb 9th, 2005, 4:45 PM   #4
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
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
dark_omen is offline   Reply With Quote
Old Feb 9th, 2005, 7:25 PM   #5
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
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;?>">
See how I broke up the input tag and used php to "echo" the value of $myVariable into it? When the final HTML is sent to the client, it will look like this:

<input type="text" name="user" value="myValue">
(remember how inputName had the string "myValue" for its data up top?)

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?
EdSalamander is offline   Reply With Quote
Old Feb 10th, 2005, 3:27 PM   #6
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
If you don't mind can you show me the javascript side. Thanks for the information of php too.
dark_omen is offline   Reply With Quote
Old Feb 12th, 2005, 8:46 PM   #7
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
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);
I dont know where that goes in the html code. Also I don't know what action is used on the input page in the form. I am assuming it it something like action="javascript" or something like that.

Thanks
dark_omen is offline   Reply With Quote
Old Feb 19th, 2005, 8:44 PM   #8
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
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
dark_omen is offline   Reply With Quote
Old Apr 5th, 2005, 11:32 AM   #9
dayglowdave
Newbie
 
dayglowdave's Avatar
 
Join Date: Apr 2005
Location: Kansas
Posts: 22
Rep Power: 0 dayglowdave is on a distinguished road
if you use $_POST over $_GET it'll be more secure , so I've heard. Would anyone care to elucidate?
dayglowdave is offline   Reply With Quote
Old Apr 5th, 2005, 3:37 PM   #10
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
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!
Pizentios is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:48 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC