In this tutorial I will show you how you can take data from a HTML form and send it to PHP to work with it. We will make also a small script for file upload, just as an example to the tutorial.
Form tag has the attributes: ‘action’ and ‘method’ ; every field from the form will have a name. Action is reffering to the file that deals with the sent data; method indicates the how, in what way the data is send to the server : ‘GET’ or ‘POST’. If we use ‘POST’ the fields will not be seen in the users browser, in comparison to ‘GET’ which can be seen.
HTML :
and the php code :
The code above takes the data from the form and displays it into the browser.
Let’s see how we can add an ‘if statement’ to see if the data entered it is correct.
Using another PHP if we can check for a password, for example. This depends to your needs.
Now let’s see a small file uploader using forms and basic PHP.
HTML :
We can see that form tag has a enctype”multipart/form-data” attribute. This is used when we are dealing with data. Also type of input is file.
Now the PHP code:
We are taking the size of the file in $a .
We are taking the original name of the file in $b .
The temporary name of the file goes in $d .
And finally in $d is the type.
I am using also an if statement to check if the size is smaller than 10000 and greater than 0. I invite to write the script and modify it to accept some given types of data taken from the MySQL database. That’s it! .
Thanks for taking the time to read this easy tutorial that explained the basics on how to deal with data taken from forms.
Leave a Reply