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. php cms reviews - find the right cms for you. 24 Quotes - Jack Bauer, Tony Almeida - Season 6 Adapt Software Political Yard - Talk about politics!
Insane Visions
Insane Visions
Insane Visions - Navigation

Favorites

Get Firefox!

PHP

MySQL

Affiliates


Gamerworld
Realm of Gaming
Game Screenie
Video Game Cheats

AllConsoleGamers
TalkPHP
XBOX 360 News
Hilarious Videos
Free Myspace Stuff

[ Apply for Affiliation ]



Scripts.com

MaxTutorial.com - Best photoshop, flash and php tutorials


Your Link Here
Your Link Here
Your Link Here

Acceptance Mark
Insane Visions - Top Navigation


Insane Visions - PHP and Forms Tutorial :: Premium PHP Scripts - AdaptCMS, AdaptBB, OneCMS PHP and Forms at Apr 04, 08 - 6: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: 11,815
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

<?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.";
$error $error 1;
}

if (
strlen($_POST['field3']) > 200) {
echo 
"Sorry, but there is a max of 200 characters.";
$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:

Vote for Article:



TutorialSelect.com, Apr 04, 08 - 10:20 pm

PHP and Forms Tutorial - Trackback

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.



Guest, Apr 10, 08 - 5:06 am
yrtyrty


 email

 website







Username:
Password:
Insane Visions - Login Register

AdaptCMS

AdaptBB

OneCMS

Latest Tutorials

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


Latest Blogs

- AdaptBB - 1.0 Features
- Starting on AdaptBB
- AdaptCMS 1.3 - June 13th


Poll of the Month

What type of software do you normally use?

Commercial
Open Source
Neither, I write my own
Results



Latest Posts

- AdaptCMS 1.4 -
Suggestions?
- Re: OneCMS v2.6 -
Ideas
- Re: Category
Templates?


Testimonials

I have dealt with many CMSes before. I have had my fair share of disappointments. At first, I had a rough time with OneCMS. Thanks to the awesome support with OneCMS, I am now gliding along easily. Even making modifications! I have never dealt with this level of support, especially on a free program, before. I think that Chuck shows great devotion, and props on the CMS.

|||||1 - Scott Berlin, Founder/Webmaster, Wired-Gamers.com


Powered by AdaptCMS Pro
Insane Visions - Footer
Personal Loans - Loans - Debt Consolidation - Unsecured Loans