FireFox! The PHP Forum Loans and Credit
Panama Web Design for Hire Free Insurance Quotes!
Web Hosting Advertise Here $10 a Month Designer Children
Never Pay Taxes Again HGH Domain name registration
Web Hosting and Dedicated Servers Insurance Affordable web-hosting


HomeWatched TopicsRegisterSearchDirectory
FAQMemberlistUsergroupsLog inStoresItemsBank
Google

Reply to topic Page 1 of 1
Working with forms in PHP
Message  

Reply with quote
Post Working with forms in PHP 
Forms and PHP
One of the most popular ways to make a web site interactive is the use of forms. With forms you can have users register for different things, submit any information you can imagine, upload files, and all types of other things. In this tutorial, we will go through the basics of forms and how to handle the data that comes from those forms with PHP.

For the scope of this tutorial, I will assume that you have a basic knowledge of HTML and PHP. Though, you can probably get by without a great understanding of either. I will cover several different form elements, including textboxes, checkboxes, select (or drop-down) boxes, radio buttons, and textareas.

In this tutorial, I am going to show you how to access the information that has been entered into a form. It is up to you to decide what you want to do with it. You can put that information into a database, a flat file, email it off, or countless other things. All I will be showing you is how to get to that data.

Ok, enough of that. Let's get to it!

Source: http://codewalkers.com/tutorials/12/1.html

View user's profile Send private message

Reply with quote
Post  
A simple form
OK, let's start off with a very basic form. Just a single button.

<HTML>
<HEAD>
<TITLE>A simple form</TITLE>
</HEAD>
<BODY>
<FORM method="POST" action="form.php">
<INPUT type="submit" name="mybutton" value="Click me!">
</FORM>
</BODY>
</HTML>  


OK, put that into a file and call it whatever you want. I will be calling it form.html. That will give you a very basic form with one button on it. You can see the "action" of the form is "form.php". That means that whatever data the form contains will be sent to the script form.php. The next step is to create the script form.php.

<HTML>
<HEAD>
<TITLE>Doing something with the form</TITLE>
</HEAD>
<BODY>
<?
if(isset($_POST['mybutton'])) {
    echo "Great! You clicked the button!\n";
} else {
    echo "Hmm...you must have come to this script without clicking the button.\n";
}
?>
</BODY>
</HTML>  


Save that code as form.php in the same directory as form.html. Now when you bring up form.html and click the button, you should get a message saying "Great! You clicked the button!".

Let's examine the code in form.php a little bit. We are going to be using this code for the basis of everything else in this tutorial. The first few lines are self explanatory. They are just the HTML needed for a proper HTML page. The first code is:

if(isset($_POST['mybutton'])) {  


What that is doing is checking to see if the variable $_POST['mybutton'] has been assigned a value or not. If it has, the "if statement" evaluates as true and then the line:

echo "Great! You clicked the button!\n";  


is executed. If the "if statement" evaluates as false then the "else" portion of that statement is executed:

echo "Hmm...you must have come to this script without clicking the button.\n";  


So what is this $_POST stuff? $_POST is a superglobal array. This array is available to a script at anytime no matter what the scope. If you aren't sure what scope is, don't worry about it right now. Just realize that you can use $_POST anywhere in your script. Anything submitted via the POST method is available in the $_POST array.

You may have noticed that the index of the $_POST array is the name we gave to the HTML form element. Whatever you call your form element (a form element being a button, textbox, checkbox, etc), that is what the index of the $_POST array will be in order to access the data from that element.

The $_POST array is only available in PHP versions 4.1.0 and later. If you are using an earlier version, you will need to use the $HTTP_POST_VARS array. The $HTTP_POST_VARS array works the same as the $_POST array, except it is not a superglobal. That is, it is not available in all scopes. In fact, if register_globals is turned off in your php.ini you will need to put "global $HTTP_POST_VARS;" at the top of the form.php script for it to work.

Source: http://codewalkers.com/tutorials/12/2.html

View user's profile Send private message

Reply with quote
Post  
Text Boxes
Now, we will add another element to our form - a textbox. In form.html, after the line:

<FORM method="POST" action="form.php">  


add the line:

Fill in the text box: <INPUT type="text" name="mytextbox" size="20"><BR>  


This will add a text box called "mytextbox" that is 20 characters long to our simple form. Now, let's do something with the data that is entered in the textbox in out form.php script. After this line in form.php:

echo "Great! You clicked the button!\n";  


add this line:

echo "<BR>You typed <b>" . $_POST['mytextbox'] . "</b> in the textbox.\n";  


Now, go ahead and try it out again. As you can see, we have passed the data in the textbox from the first page to the next. It really is that simple. This is basically how it is going to work for each of the other form elements. There are a few subtle differences, but we will go through those.
Text Boxes
Now, we will add another element to our form - a textbox. In form.html, after the line:

<FORM method="POST" action="form.php">  


add the line:

Fill in the text box: <INPUT type="text" name="mytextbox" size="20"><BR>  


This will add a text box called "mytextbox" that is 20 characters long to our simple form. Now, let's do something with the data that is entered in the textbox in out form.php script. After this line in form.php:

echo "Great! You clicked the button!\n";  


add this line:

echo "<BR>You typed <b>" . $_POST['mytextbox'] . "</b> in the textbox.\n";  


Now, go ahead and try it out again. As you can see, we have passed the data in the textbox from the first page to the next. It really is that simple. This is basically how it is going to work for each of the other form elements. There are a few subtle differences, but we will go through those.

Source: http://codewalkers.com/tutorials/12/3.html

View user's profile Send private message

Reply with quote
Post  
Checkboxes
Now let's take a look at checkboxes. First a very simple checkbox situation - just a single checkbox. In form.html, after the line:

Fill in the text box: <INPUT type="text" name="mytextbox" size="20"><BR>  


add this line:

Check me or not: <INPUT type="checkbox" name="mycheckbox" value="1"><BR>  


In form.php after the line:

echo "<BR>You typed <b>" . $_POST['mytextbox'] . "</b> in the textbox.\n";  


add the lines:

if(isset($_POST['mycheckbox'])) {
    echo "<BR>You checked the checkbox.\n";
} else {
    echo "<BR>You didn't check the checkbox.\n";
}  


Ok, that was a very simple checkbox example, let's do something with multiple checkboxes. In form.html, change the line:

Check me or not: <INPUT type="checkbox" name="mycheckbox" value="1"><BR>  


to read:

Check me or not: <INPUT type="checkbox" name="mycheckbox[]" value="1"><BR>  


Notice we have added opening and closing brackets [] to the name of the checkbox. This will create an array for our checkbox values. Now, let's add a couple more checkboxes. After the current checkbox line in form.html, add these lines:

Check me or not: <INPUT type="checkbox" name="mycheckbox[]" value="2"><BR>
Check me or not: <INPUT type="checkbox" name="mycheckbox[]" value="3"><BR>  


In form.php we will now need to determine which checkboxes have been checked. Luckily for us, PHP gives us an easy way to loop through all the values in an array - a foreach loop. Continuing along in our form.php script, add these lines after the last ones we added:

foreach($_POST['mycheckbox'] as $value) {
    echo "<BR>You clicked checkbox number " . $value , "\n";
}  


If you type asdasd in the textbox and check checkboxes number 1 and 3, your output should look like:

Great! You clicked the button!
You typed asdasd in the textbox.
You checked the checkbox.
You clicked checkbox number 1
You clicked checkbox number 3

In form.html, you could very easily change the values of the checkboxes to anything you like. If you changed the value of checkbox 3 to dog, your output would look like:

Great! You clicked the button!
You typed asdasd in the textbox.
You checked the checkbox.
You clicked checkbox number 1
You clicked checkbox number dog

That really doesn't make sense, but it shows you how the data is stored. The value that we are printing out isn't necessarily a numeric value. It is whatever value we assign that checkbox.

Source: http://codewalkers.com/tutorials/12/4.html

View user's profile Send private message

Reply with quote
Post  
Radio Buttons
Radio buttons are very similar to checkboxes. The only real difference is that when you call radio buttons by the same name, only one of them can be selected. That really makes our life easier, as we don't have to loop through and figure out which ones are selected. Add this next to form.html:

Choose one:
Blue <INPUT type="radio" name="myradio" value="blue" CHECKED>
Green <INPUT type="radio" name="myradio" value="green">
Yellow <INPUT type="radio" name="myradio" value="yellow"><BR>  


And to form.php:

echo "<BR>The color you picked was " . $_POST['myradio'] . "\n";  


Very straightforward, just like the others.

Source: http://codewalkers.com/tutorials/12/5.html

View user's profile Send private message

Reply with quote
Post  
Select Boxes
On to select boxes or as they are sometimes called, drop-down lists. The way these work is exactly the same as the other elements we have covered so far. Really, once you know one, you know them all! In form.html, add these lines next:

Select something from this list:
<SELECT name="myselectbox">
<OPTION value="dog">Dog</OPTION>
<OPTION value="cat">Cat</OPTION>
<OPTION value="pig">Pig</OPTION>
</SELECT><BR>  


And in the form.php script add this next:

echo "<BR>" . $_POST['myselectbox'] . " was chosen from the select list.\n";  


Select boxes are just as simple as that. One thing to note is that if you do this:

<OPTION value="cat">Dog</OPTION>  


cat is what will be passed to the form.php script. Dog is only there to display, cat is the value assigned to that item in the list. This can be useful if you want to display a list of items to a user, but assign a numerical value to this items, like:

<SELECT name="myselectbox">
<OPTION value="1">One thing</OPTION>
<OPTION value="2">Another thing</OPTION>
<OPTION value="3">Something totally different</OPTION>
</SELECT><BR>  

Source: http://codewalkers.com/tutorials/12/6.html

View user's profile Send private message

Reply with quote
Post  
Textareas
Well, guess what? Textareas are handled in the exact same way as these other elements are. So, let's just dive right in. In form.html:

<TEXTAREA name="mytextarea" rows="4" cols="30"></TEXTAREA><BR>  


and in form.php:

echo "<BR>In the textarea, you typed: " . $_POST['mytextarea'] . "\n";  

Source: http://codewalkers.com/tutorials/12/7.html

View user's profile Send private message

Reply with quote
Post  
Conclusion
That's it. Nothing really to forms and PHP. PHP just makes it so simple it is unbelievable. Below are the final versions of form.html and form.php. If you have any questions about this subject, ask it in our forum. Thanks for stopping by!

<HTML>
<HEAD>
<TITLE>A simple form</TITLE>
</HEAD>
<BODY>
<FORM method="POST" action="form.php">
Fill in the text box: <INPUT type="text" name="mytextbox" size="20"><BR>
Check me or not: <INPUT type="checkbox" name="mycheckbox[]" value="1"><BR>
Check me or not: <INPUT type="checkbox" name="mycheckbox[]" value="2"><BR>
Check me or not: <INPUT type="checkbox" name="mycheckbox[]" value="3"><BR>
Choose one:
Blue <INPUT type="radio" name="myradio" value="blue" CHECKED>
Green <INPUT type="radio" name="myradio" value="green">
Yellow <INPUT type="radio" name="myradio" value="yellow"><BR>
Select something from this list:
<SELECT name="myselectbox">
<OPTION value="dog">Dog</OPTION>
<OPTION value="cat">Cat</OPTION>
<OPTION value="pig">Pig</OPTION>
</SELECT><BR>
<TEXTAREA name="mytextarea" rows="4" cols="30"></TEXTAREA><BR>
<INPUT type="submit" name="mybutton" value="Click me!">
</FORM>
</BODY>
</HTML>  

<HTML>
<HEAD>
<TITLE>Doing something with the form</TITLE>
</HEAD>
<BODY>
<?
if(isset($_POST['mybutton'])) {
    echo "Great! You clicked the button!\n";
    echo "<BR>You typed <b>" . $_POST['mytextbox'] . "</b> in the textbox.\n";
    if(isset($_POST['mycheckbox'])) {
        echo "<BR>You checked the checkbox.\n";
    } else {
        echo "<BR>You didn't check the checkbox.\n";
    }
    foreach($_POST['mycheckbox'] as $value) {
        echo "<BR>You clicked checkbox number " . $value . "\n";
    }
    echo "<BR>The color you picked was " . $_POST['myradio'] . "\n";
    echo "<BR>" . $_POST['myselectbox'] . " was chosen from the select list.\n";
    echo "<BR>In the textarea, you typed: " . $_POST['mytextarea'] . "\n";
} else {
    echo "Hmm...you must have come to this script without clicking the button.\n";
}
?>
</BODY>
</HTML>  

Source: http://codewalkers.com/tutorials/12/8.html

View user's profile Send private message
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
  



Google

FireFox! The PHP Forum Loans and Credit
Panama Web Design for Hire Free Insurance Quotes!
Web Hosting Advertise Here $10 a Month Designer Children
Never Pay Taxes Again HGH Domain name registration
Web Hosting and Dedicated Servers Insurance Affordable web-hosting


Web Design by PlatinumShore.com & Web Hosting by TradeWebHosting.com