Home arrow PHP arrow Page 6 - Website Database Basics With PHP and MySQL

Checkboxes and other HTML form processing - PHP

The World Wide Web (WWW) does only one thing - provide information. If you have information about something, you can share it with the world by building a website. As your website grows you may run into two problems: Your website has so much information that visitors can't quickly find what they want and visitors want to give you information. Both of these problems can be solved by building a database on a website. This introductory article shows you how to do this using basic PHP-MySQL interaction.

TABLE OF CONTENTS:
  1. Website Database Basics With PHP and MySQL
  2. HTML talks to PHP talks to MySQL
  3. Verifying form data
  4. Using cookies to identify and track visitors
  5. Weird SQL: What The Books Don't Tell You
  6. Checkboxes and other HTML form processing
By: Thomas Kehoe
Rating: starstarstarstarstar / 100
January 11, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
HTML forms are easy to design, as long as you allow one value per field. When you allow more than one value, the processing gets tricky.
  1. Checkboxes
  2. SELECT multiple scrolling lists
  3. Searching with multiple values

Checkboxes

Checkboxes are the simplest way to allow users to enter more than one value into a field:
What pets do you have?
Dog
Cat
Fish
You can check one, two, or all of the pets. The HTML code looks like this:


What pets do you have? <FORM> <INPUT TYPE=checkbox NAME=PET_ARRAY[] value=dog> Dog<br> <INPUT TYPE=checkbox NAME=PET_ARRAY[] value=cat> Cat<br> <INPUT TYPE=checkbox NAME=PET_ARRAY[] value=fish> Fish<br> </FORM>


The MySQL field name is PET, but here we use PET_ARRAY[]. When the user clicks the SUBMIT button, the values are passed to the header looking like this:


http://www.mywebsite.com/myform.php3?PET_ARRAY%5B%5D=dog&PET_ARRAY%5B%5D=cat


5B is 91 in hexadecimal, and the HTML character entity for left square bracket — [ — is &#091, so %5B means left square bracket. 5D is 93 in hexadecimal, and the HTML character entity for right square bracket — ] — is &#093, so %5D means right square bracket.

When we get to the PHP script that processes this form, we use this script to put both values into one field. The PET field is a SET datatype.


if ($PET_ARRAY) { $PET = implode($PET_ARRAY, ","); $result = mysql_query ("UPDATE dbname SET PET = '$PET' "); if(!$result) { echo "<B>UPDATE unsuccessful:</b> ", mysql_error(); exit; } }


if ($PET_ARRAY) checks if the user checked any of the boxes. If the user doesn't have any pets, the field is left empty.

$PET = implode($PET_ARRAY, ","); converts the array into a string, with the elements separated by commas. The values passed to the header above would come out as


dog,cat


The query then puts the string into the PET of the database.

To search a SET datatype, remember to put % wildcards before and after the search value. This is necessary to find one of several values, and ignore the commas. E.g.,


SELECT * FROM dbname WHERE PET LIKE '%$PET%';



SELECT multiple scrolling lists

Another way to allow selection of more than one value is to use pull-down menus or scrolling lists. E.g.,
What pets do you have?



<FORM> What pets do you have? <FORM> <SELECT NAME=PET_ARRAY[] size=5 multiple> <option>Dog <option>Cat <option>Fish <option>Kangaroo <option>Ptarmigan <option>3-Toed Sloth <option>Lemur <option>Narwhal </select> </FORM>



If you shift-click, you can select two or more adjacent values, e.g., Dog, Cat, and Fish. With Windows, you ctrl-click to select two or more non-adjacent values. On the Macintosh you hold down the butterfly key as you click your mouse.

99.999% of Internet users don't know this, and 99.99% aren't going to read instructions you provide, so I don't use SELECT multiple scrolling lists. Checkboxes are more obvious.


Searching with multiple values

This is another short section. I haven't figured out how to search for more than one value, e.g., allow users to query a SET of pets to find people with a dog and a fish. If I figure it out I'll explain it here. {mospagebreak title=Using include() to bring in outside files} Sometimes you have a block of text that is repeated on many webpages. For example, all of these chapters start with "PHP and MySQL Website Database Basics" and the copyright, etc. Later you need to change one word, and you have to go into 20 documents to change the same word.

A better way is to put the block of text into its own document, and use include() to bring the text into each webpage. Any change you make in the text document will appear in all your webpages.

  1. include() with text
  2. include() with applets
  3. Using <object>

include() withtext

Write the text object as a PHP file, e.g.,


<?php print "This is my text block."; ?>


Save it as a PHP files, e.g., textblock.php3.

In each webpage, you put in this PHP function:


include ('textblock.php');



include() with applets

include() can do more than text. Any PHP file can be included. You should be able to call other types of files, but I haven't tried this.


Using <object>

HTML's code <object> is another way to include external files. It was created for applets, such as Java and Microsoft's ActiveX, but supposedly works with other types of files. I've never gotten it to work.

 
 
>>> More PHP Articles          >>> More By Thomas Kehoe
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: