PHP and Forms - Insane Visions

PHP and Forms

We finally have a new tutorial up, this time covering the basics of using PHP and Forms, take a look:

“You probably have a CMS or at least a blog script running on your website and to add articles, among other things, you fill out a form. If you’ve ever wondered how that translated over to a blog being added to a database and how it’s verified, then you will want to read this tutorial.”

PHP and Forms – Tutorial [View]

http://www.insanevisions.com/tutorials/phpforms.phps

You probably have a CMS or at least a blog script running on your website and to add articles, among other things, you fill out a form. If you’ve ever wondered how that translated over to a blog being added to a database and how it’s verified, then you will want to read this tutorial.

I’ll go over the simple passing of data from form to adding it to the database and the different functions and variables that can be used.

The Form and Query


<?php

if ($_GET['view'] == "form") {

echo 
"<form action="form.php?view=query" method="post">

Testing Field 1 <input type="text" name="field1"><br>

Testing Field 2 <input type="text" name="field2"><br>

<textarea name="field3" cols="30" rows="15"></textarea><br>

<input type="submit" value="Submit Form"></form>";

}

if ($_GET['view'] == "query") {

print_r($_POST);

}

?>

Okay so knowing HTML, you will notice the very basic form we have, 2 text fields and a textarea. Now in the form action we have set it to ‘Post’, we’ll show the usage of ‘GET’, later on. Now on the query page we are using the ‘print_r’ function, this PHP function just simply prints out all variable data. Using it to print out the Post values, it will list the names of the fields (field1, field2, field3) and the values entered. Next we’ll show the database use.

Database and GET


<?php

if ($_GET['view'] == "form") {

echo 
"<form action="form.php?view=query&id=7" method="post">

Testing Field 1 <input type="text" name="field1"><br>

Testing Field 2 <input type="text" name="field2"><br>

<textarea name="field3" cols="30" rows="15"></textarea><br>

<input type="submit" value="Submit Form"></form>";

}


if ($_GET['view'] == "query" && $_GET['id']) {




mysql_query
("INSERT INTO `table` VALUES (null, '".addslashes($_POST['field1'])."',
'"
.addslashes($_POST['field2'])."', '".addslashes($_POST['field3'])."',
 '"
.$_GET['id']."')"); 

}

?>


In this example we have the same basic form, but have modified the URL so we can show GET. In the query example we are using the basic $_POST function, entering the data into the database. Addslashes ensures the data is entered all right, escaping it properly. Another bit you may notice that’s different is that we added another check to the if statement, the query part will run in the browser if the id variable isn’t blank.

GET simply gets (get it? all right, I’ll stop) variables in the URL. You can set it in the form (as we did for the action), or by setting the method of the form to ‘get’, all fields passed will have there data passed onto the URL. So if you have a search form, you could see a URL like so – search.php?action=search&search=Hey+There+buddy. Any hidden fields will be passed on as well into the URL, hence GET.

Form Verification


<?php

if ($_GET['view'] == "form") {

echo 
"<form action="form.php?view=query&id=7" method="post">

Testing Field 1 <input type="text" name="field1"><br>

Testing Field 2 <input type="text" name="field2"><br>

<textarea name="field3" cols="30" rows="15"></textarea><br>

<input type="submit" value="Submit Form"></form>";

}


if ($_GET['view'] == "query" && $_GET['id']) {

$error 0;

if ($_POST['field1'] == "") {

echo 
"Sorry, but field1 was not entered.";

$error $error 1;

}

if (strlen($_POST['field2']) < 10) {

echo "Sorry, but you need more than 10 characters for field2.";

$error $error 1;

}

if (strlen($_POST['field3']) > 200) {

echo 
"Sorry, but there is a max of 200 characters for field3.";

$error $error 1;

}

if ($error == 0) {



mysql_query
("INSERT INTO `table` VALUES (null, '".addslashes($_POST['field1'])."',
 '"
.addslashes($_POST['field2'])."',

'".addslashes($_POST['field3'])."', '".$_GET['id']."')"); 

}
}

?>


This looks really similar to the last example, however we do verification on field1, field2 and field3. We first see if field1 is blank, if so we add a value of 1 to the $error variable. In the end we will run the query only if the $error variable has a value of 0, meaning no errors occured.

With field2, we use a PHP function called ‘strlen’. Strlen simply returns the number of characters passed, this can be used to have a functioning minumum or maximum amount of characters for a field, no javascript required!

Conclusion

Using forms with any PHP/MySQL software is extremely common and at times, required. From polling scripts, to the basic functions of scripts, it’s a staple in PHP and extremely important to be proficient in. Happy coding!

James

James

Hi there, I'm James, editor in chief at InsaneVisions.com and finance journalist. Feel free to message me on Twitter and Facebook. Make sure to subscribe to our mailing visit for the latest finance and tech news.
James

Latest posts by James (see all)