Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - 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 - Checkboxes


    (Page 4 of 7 )

    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 1You clicked checkbox number 3In 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 1You clicked checkbox number dogThat 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.

    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 5 Hosted by Hostway
    Stay green...Green IT