How to Get User's Input

03-08-2008
Posted in: Programming/PHP by: sumario

Want to know how to get data from your users and stuff?

Nice and easy one here. How to get a users input from a form and/or query string.

Say we have this form:

<form method="GET">

<input type="text" name="car" />

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

</form>

Now, first I have to say, there are two methods of sending and receiving data. One method is sending the data into the query string of the URL. The query string part of the URL is after the ? in this example, http://www.sumario.co.uk/index.php?car=rover.

The other method is sending the data into the memory. Each method is good depending on the situation and data being sent. Sending data into the memory is more secure and allows for more space especially for tasks such as uploading images. Sending data into the query string is limited to the amount of data and can be easily manipulated by 3rd party users etc.

I won't go to much into the details of sending the data, but:

Query is labelled as GET & Memory is labelled as POST, replace the method in the form with which ever you want.

To get data from the query string, you would use the following code:

$car = $_GET['car'];

This section, "['car']" is the actual name of the string. So in terms of the suggested URL, item is the name. So the value of car will go into my selected variable, $car.

When data has been sent into the memory using a form like above, the segment of code is exactly the same as above except instead of GET, POST is used.

$car = $_POST['car'];

Again, name is the actual name of the text field in the form and the value goes into my selected variable.

I think thats all hehe.

So, now we got the users input, you can use it now for what ever you want, like searching a database. But a word or warning....you cannot trust your users. When receiving data from users or getting data that can be easily manipulated by your users, you must validate and sanitize the data before you use it in your main script. If you don't, you may be a victim to such attacks as SQL Injection. I can go into this at a later date.

Enjoy for now.

Bookmark and Share

There are no comments.

About this Site

What this site is mainly about is a learning experience for me, I love to program so I use this site to just mess about with and program stuff (well use too, not enough time now). One great feature this site offers is an Image Hosting for free to all users, registered and non registered. This site is now an Image Hosting provider first and a random tutorial/bloggish second.

Register now for free and get more features when using the Image Host.

Login Username:

Password:

Remember me next time.
Register
Upload an Image


Comments:
Thumbnail Size:


Clicking the Submit button means you agree to our Terms & Conditions.
Search

Sub-Categories
-->
Stats
  • There are currently 1901 images hosted.
  • 586.86MB of space has been used.
  • 295,059.84MB of bandwidth has been used.
  • Images have been downloaded 751289 times.
  • There are 89 registered users.
  • There are currently 18 articles posted
Sumario © 2008 - Terms & Conditions - RSS Feed
cronage