Miscellaneous
  Home arrow Miscellaneous arrow Page 2 - Working with forms in PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Working with forms in PHP
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 6
    2002-06-30

    Table of Contents:
  • Working with forms in PHP
  • A simple form
  • Text Boxes
  • Checkboxes
  • Radio Buttons
  • Textareas
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with forms in PHP - A simple form


    (Page 2 of 7 )

    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.

    More Miscellaneous Articles
    More By Matt Wade


       · afafas;hiiu
       · You didn't over complicate a rather simple subject.Taught me everything I...
       · 
       · Hi, wz wonderin how i would submit the foreach() loop used for checkboxes to an...
       · Hope this works
       · Hi, I don't know if anyone responded to the first comment titled "how to...
       · If you don't check one of the 3 checkboxes in the form the foreach code generates an...
       · Easy to understand.Thanks for taking the time to share your knowlege.
       · Woohoo! I fixed the "error!"Instead of posting the code after...
       · Thanks for spending the time making this tutorial. It has assisted me...
       · This is great...
       · This is great...
       · 
       · the code: "if(isset($_POST['mycheckbox'])) { echo...
       · I'm new to this, so what do you mean by like a file, where do i save it, and its...
       · A great starter, clear and concise. Enough information to get a newbie like me...
     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT