• Home
  • About
  • Archives
  • Icon Gallery
Subscribe: Posts | Comments | E-mail
  • 'Net Culture
  • Downloads
  • Music
  • PHP/mySQL
  • Teh Funneh
  • Tools
  • Video
  • Web Dev

Snipe.Net

Posts Tagged ‘forms’


Posted on June 19, 2006 - by snipe

Checkboxes/Multiple Select Boxes in PHP

Checkboxes/Multiple Select Boxes in PHP

For the PHP newbie, checkboxes and/or multiple select listboxes can be baffling in the beginning. It’s actually not very hard at all, and is often one of the PHP newbie’s first experience with arrays.

The logic behind checkboxes and multiple select listboxes is identical. Because of this, we’ll get the HTML bit of it done for both:

A. Checkboxes
<input type=”checkbox” name=”foo[]” value=”1″>
<input type=”checkbox” name=”foo[]” value=”2″>
<input type=”checkbox” name=”foo[]” value=”3″>
(etc….)

B. Multiple Select Listboxes
<select name=”foo[]” size=”4″ multiple>
<option value=”apples”>Apples</option>
<option value=”oranges”>Oranges</option>
<option value=”pears”>Pears</option>
<option value=”grapes”>Grapes</option>
<option value=”mangos”>Mangos</option>
</select>

Getting the data out

Now we just have to write the PHP code that will be able to extract the data the user selected from the array we created ($foo[])

<?php

// check to be sure at least one option was selected
$foo = $_POST['foo'];
if (
count($foo) > 0) {
// loop through the array
for ($i=0;$i<count($foo);$i++) {

// do something - this can be a SQL query,
// echoing data to the browser, or whatever
echo “<li>$foo[$i] \n”;

} // end “for” loop

} // endif

?>


1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 3.5 out of 5)
Loading ... Loading ...


  • Categories

  • What I'm Doing...

    • was stoked about her progress until the US armory server locked her out again. http://apps.facebook.com/wow_toons/ 2 hrs ago
    • is still unsure how she feels about people telling her to increase her personal brand. Marketing people... sheesh... 2 hrs ago
    • is making the WoW Armory and Facebook her bitch while on the bus. The powah! 5 hrs ago
    • More updates...
  • Random Thing You Probably Didn't Know About Me

    • I was once a make-up artist for a pr0nographer
  • Make With the Clicky!

  • Flickr Photos

  • AJAX/Web 2.0

    • AJAXDaddy
    • Noupe
  • CSS

    • Blueprint CSS
    • Noupe
  • Geek Humor

    • Bash.Org
    • Daily WTF
    • Diesel Sweeties
    • FailBlog
    • Penny Arcade
    • xkcd
  • Graphics

    • Adobe Kuler
    • Iconspedia
    • Photoshop Express
    • Smashing Magazine
  • Life Tools

    • LifeHacker
  • Misc

    • 419 Eater
    • Cellphone PSA Cards
    • Glarkware
    • TehAwesome
    • What’s That Bug?
  • Music

    • Hipster, Please!
    • Jonathan Coulton
    • MC Frontalot
    • MC Lars
    • Optimus Rhyme
  • PHP/mySQL

    • PHPBuilder
    • Zend
© 2008 Snipe.Net - Bitterness never tasted so sweet
The Papercut theme by WooThemes - Premium Wordpress Themes