NYCPHP Meetup

NYPHP.org

form confusion

George Webb gw.nyphp at gwprogramming.com
Wed May 14 16:56:19 EDT 2003


Joshua, here are some basic thoughts about FORMS and PHP
as they may relate to your examples:

	1/ Groups of radio buttons should have the same "NAME"
	attribute, because that's what links them together to be
	a one-of-many selector.  Also, in your PHP script, you want 
	to read the one single value result of the radio group --
	in your case "structural stability" is the name.
	These work very similarly to a <SELECT> list (*without*
	the MULTIPLE attribute) with <OPTIONS> for each option.

	2/ Space characters in form field names is sometimes
	problematic... better to stay with alphanumeric and
	underscore if possible.  This is especially true if
	you want your form field names to match up with names of
	your database columns.  Databases usually only allow
	A-Za-z0-9_ for column names.

	3/ Unlike radio buttons, checkboxes should each have
	*unique* names.  E.g.:

	<INPUT TYPE=CHECKBOX NAME="inadequate_storage" VALUE=1>...
	<INPUT TYPE=CHECKBOX NAME="insect_activity" VALUE=1>...

	The "VALUE" HTML attribute should probably just be "1",
	so that you can easily check whether an input element is
	checked or not, e.g:

	if ( @$_POST['inadequate_storage'] ) {
		/*** handle "checked" ***/
	}
	if ( @$_POST['insect_activity'] ) {
		/*** handle "checked" ***/
	}

	(The at-sign in front suppresses warnings when the checkbox
	is not set (and error_reporting is E_ALL).)

	4/ If you have a group of "related" checkboxes, for example
	a question like "Check all that apply...", you might want
	to use an input array as follows:  set the NAME attributes
	to all the same thing; put an empty square-brackets on the
	end of each name, to make it an array; set the VALUE attribute
	of each one to a unique value, e.g.:

	Bands I Like:
	<INPUT TYPE=CHECKBOX NAME="bands[]" VALUE="Santana">Santana
	<INPUT TYPE=CHECKBOX NAME="bands[]" VALUE="n'sync">n'Sync
	<INPUT TYPE=CHECKBOX NAME="bands[]" VALUE="John Tesh">John Tesh
	<INPUT TYPE=CHECKBOX NAME="bands[]" VALUE="Ween">Ween

	Then you would get the user's checked values very easily from
	PHP:

	$checked_values_array = $_POST['bands'];

	Or if you want to print them out:
	foreach ( $_POST['bands'] as $band ) {
		echo "He likes $band<BR>\
";
	}

	Related Checkboxes work very similarly to a <SELECT MULTIPLE>
	element with <OPTIONS> for each option you can check.

	It's FUN WITH FORMS, not FORM CONFUSION!!!


	5/ There are so many different ways to do the database
	side for radio buttons and checkboxes.  One main issue is,
	will one "entry cell" have exactly one value, or possibly
	multiple simultaneous values?  If it's exactly one, you
	can simply store the result as a text field, or if it's
	yes/no, maybe as an enumerated type or integer, e.g.:

		text
		enum('YES','NO') NOT NULL
		enum('Y')
		int NOT NULL
	
	If there might be multiple simultaneous choices, like the
	bands example above, then you might want to create a new
	table with the user ID (or specimen ID) in the first column,
	and the value in the second.  The results might come to look
	like this table:

		+-----+----------------+
		| ID  | band           |
		+-----+----------------+
		|  3  | John Tesh      |
		| 19  | John Tesh      |
		|  6  | Ween           |
		|  6  | John Tesh      |
		| 14  | Santana        |
		|  9  | n'Sync         |
		| 14  | John Tesh      |
		+-----+----------------+
	
	This way, each "ID" can have as many "bands" as it wants.

	A much cheesier, but somewhat simpler way is to hard-code
	each band name as a field into the main table, e.g.

	+----+----------+-------------------------------------------------------+
	| ID | stuff    | likes_santana | likes_nsync | likes_tesh | likes_ween |
	+----+----------+-------------------------------------------------------+
	|  3 | foo      | NO            | NO          | YES        | NO         |
	|  6 | bar      | NO            | NO          | YES        | YES        |
	|  9 | baz      | NO            | YES         | NO         | NO         |
	| 14 | quux     | YES           | NO          | YES        | NO         |
	| 19 | xyzzy    | NO            | NO          | YES        | NO         |
	+----+----------+-------------------------------------------------------+

	It's FUN WITH DATABASES.

	6/ One other database consideration is maybe some users want to
	download the entire database into a spreadsheet or MS Access or
	something.  E.g. the users there might rather see the results in the
	cheesier method above, if they're more into looking at single
	huge spreadsheets.  Or if they like Access, they might like the
	other way.  Or maybe you matter the most, because YOU have to
	write the PHP queries to display the results -- so do what's
	easiest for you to work with.

	7/ If you're not sure about what to do, don't worry you may
	likely want to change it later after you get it.  Just make it
	work and get it done fast.  PHP should very helpful with that.
	Even with advanced projects, the first version is usually not
	meant to be optimized; that usually comes later -- after you
	have thousands of users.


Best, George.
		
George Webb
gw.nyphp at gwprogramming.com
x1384



More information about the talk mailing list