News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

PHP Lesson 10 - basics of user input

Started by Parham, September 04, 2003, 11:31:54 PM

Previous topic - Next topic

Parham

There are a number of ways to incorporate user input into your scripts.  The single most used method of taking in user input is by using HTML forms.  Let's take a very simple example of a HTML file with forms which point to a PHP script.  I'm aware that some of you might not exactly be comfortable with HTML, if that's the case please read a few tutorials on the net about HTML.


<html>

<head>

<title></title>

</head>
<body>

<form action="http://www.website.com/script.php" method="post">
Form 1: <input type="text" name="form1"><BR>
Form 2: <input type="text" name="form2"><BR>

<input type="submit" value="submit">
</form>

</body>
</html>


The "get" method means that the form data is to be encoded by the browser into the URL and sent to the specific program.  The "post" method means that the form data is to appear within a form data set and sent to the script.  There is a general rule which states that you should use the "get" method when retrieving information and use the "post" method when sending information.

Let's say we filled in the above forms, filling in "form1" with "hi" and filling in "form2" with "bye".  If were were to submit the above form with the "post" method, we would see "http://www.website.com/script.php" in the address bar.  The information of the forms would be sent with the form data set (don't mind this too much, read more about it at http://www.w3.org/TR/REC-html40/interact/forms.html#form-data-set).  But if we were to submit the information via the "get" method, you would see your address bar change to this: "http://www.website.com/script.php?forms1=hi&forms2=bye".  The form information is encoded right inside the URL where you can see it instead of being hidden like the "post" method.  Everything after the question mark ("?") is known as the query string.  The query string holds all the user input someone submits via a form (or just manually in the URL).  Every different form is separated by an ampersand ("&") or semicolon (";") and form names and values are separated by the equal sign ("=").

Now you might ask why this is important.  Well first of all, URL caching.  "get" methods are cached while "post" methods aren't cached by your browser.  According to http://support.microsoft.com/default.aspx?scid=kb;EN-US;q208427, the "get" has a limit on the amount of information you can send by it.  Because the "get" method uses the URL to send user input, it is limited by the maximum length a URL can be (2048 characters).  The "post" method has no limitations on the amount of information you can send by it.

Because I don't want to get into the HTML of PHP scripting too much, I'm going to use the "get" method more often.  This is because, I can show you examples without directly using HTML.

For example, we can access a URL like "http://www.website.com/script.php?forms1=hi&forms2=bye&forms3=something" and right from that we can get into the PHP of "script.php".  Hope that made sense, next few lessons I'll go into detail with this stuff.

Joshua Dickerson

Parham, glad you got to this. It seems to be what most people struggle on. They have a hard time understanding the concept :)
Come work with me at Promenade Group



Need help? See the wiki. Want to help SMF? See the wiki!

Did you know you can help develop SMF? See us on Github.

How have you bettered the world today?

Parham

was this one particular lesson useful?  I wanted to lay some ground work first, give some basics before i went into detail with actual variables and stuff.  I would appreciate comments on this particular lesson because when I reread it, it seemed a little pointless.

Acf

It wasnt that pointless you made me understand what a post does and what a get does. :)
Sigh...

Advertisement: