Store Front Contact Us Projects Blog PHP Tutorials Community Forums Support Downloads Client Area Homepage Charlie Page - Home to the blog of myself! Dynasty Wizard - Free game dynasties, period. Software Reviews - Software News, Reviews and more Coming Soon... Adapt Software Rock Reviews
Insane Visions
Insane Visions
Insane Visions - Navigation

Favorites

PHP

MySQL

Friends

Solitude is Bliss

OneCMS



Scripts.com

MaxTutorial.com - Best photoshop, flash and php tutorials


Your Link Here

Insane Visions - Top Navigation


Insane Visions - PHP and Forms Tutorial :: Premium PHP Scripts - AdaptCMS, AdaptBB PHP and Forms at Apr 04, 08 - 7:42 pm
News Div
Post to Digg Post to Facebook Post to Furl Post to Netscape Post to Newsvine Post to Reddit Post to Simpy Post to Spurl Post to StumbleUpon

Views: 54,832
Type: PHP
Experience Level: Beginner

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

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

Testing Field 1 

Testing Field 2 



"
;
}

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

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

Testing Field 1 

Testing Field 2 



"
;
}

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

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

Testing Field 1 

Testing Field 2 



"
;
}

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!

Download:

Rating:



Sorry but you cannot post a comment, you do not have the necessary permissions to.
Comments
Guest, Apr 10, 08 - 6:06 am
yrtyrty
Rating: , Rate Comment: Sorry, you cannot rate this item again

admin, Dec 03, 08 - 11:27 pm
rewrer werwqr ewrqwer
Rating: , Rate Comment: Sorry, you cannot rate this item again


Page processed in 0.077 seconds.

Username:
Password:
Insane Visions - Login Register

AdaptCMS

AdaptBB

Latest Posts

PHP File Uploads and Max Size

Latest Tutorials

- Basic PHP Security
- Bot Detection with PHP
- PHP and Forms

Latest Blogs

- AdaptCMS 2.0.1 - September 10th
- AdaptCMS 2.0 - March 26th
- AdaptCMS 2.0 - January 31st


Poll of the Month

Have you used a PHP Framework?

No, not a programmer
No, not yet
CakePHP
Zend
Symfony
CodeIgniter
Other


Results


Rock Reviews



Passover Review

"The 60's and 70's rock music is dead but Passover makes you ignore that and feel that atmosphere of music again."


Testimonials

I tried five different content management systems before I settled on OneCMS. OneCMS has proved itself to be very powerful, capable and easily customizable. The support has been fantastic; any little problems or questions I had were answered in one day. This software comes with my highest recommendations.

- George Lester, Founder/Webmaster, NintendoFocus.com  




Your Ad Here
Powered by AdaptCMS
Insane Visions - Footer