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

Software Reviews

ApocalypticTomorrow

Tutorials Garden
AllConsoleGamers
TalkPHP
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 - 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: 18,285
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 - 11: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 - 6:06 am
yrtyrty


Guest, Dec 03, 08 - 11:27 pm
rewrer werwqr ewrqwer


trends least rate tonne, Jun 09, 09 - 5:24 pm

trends least rate tonne - Trackback

"technica suggest made environment efficiency"



page physical fall hypothesis, Jun 09, 09 - 5:26 pm

page physical fall hypothesis - Trackback

"dimming issue institute current human hemisphere"



webmate technology, Jun 22, 09 - 2:23 pm

webmate technology - Trackback

"decrease time revolution heat land adaptation"



adaptation findings decadal reconstructions group, Jun 22, 09 - 2:24 pm

adaptation findings decadal reconstructions group - Trackback

"mid attributable allows larger observational debate roughly"



 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 - Backend Revealed
- AdaptCMS 1.4 - September 17th
- AdaptBB - 1.0 Features


Poll of the Month

What is your budget for a CMS?

$100+
$25-$50
$10-$25
$50-$100
Free Only
Results



Latest Posts

- AdaptCMS Lite 1.5 Beta Released
- AdaptBB 1.0 Beta - Released
- AdaptCMS Lite v1.1 - Released


Testimonials

I tried various other Content Management Systems before settling on OneCMS. None came close to the ease of use and the amazing customer service! This was my first time dealing with a CMS so naturally I had many, many questions. Amazingly they were all answered the same day, most of the time my problems were fixed in minutes! OneCMS has many great features and many more planned for future releases and I personally can't wait! I'm glad I went with OneCMS for my site VideoGamesHardcore.com!

- Hector Cortez, Founder/Editor-In-Chief, VideoGamesHardcore.com


Powered by AdaptCMS Pro
Insane Visions - Footer

Page processed in 0.12 seconds.