Re: How to process a query form with CHECKBOX Please help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon 27 Dec 04,  8:48 PM, S Kumar <ps_php@xxxxxxxxx> said:
> Hello Pete:
> 
> However, in the case of checkboxes, I do not know how
> to make variables out of them depensing on what user
> selects.  Say whatever user cheks in the checkbox,
> that variable has to be searched in TABLE-C and
> various other columns in that table.  I am totally
> clueless how  I can capture what user checks as a
> variable and incorporate into the SQL statement. 
 
Hi Kumar,

I've never played with databases before 3 or 4 days ago, and maybe played
around with HTML forms a couple of times in my life.  I'm still learning the
basics of SQL, so if you don't know something about SQL, I almost certainly
don't know it!

However, if all you need to know is how to access checkboxes in PHP... that
I've played around with.  I wrote a sample program, "check.php".

Sorry for the painfully obvious comments, but I wrote them for me just as
much as for you.  :)  I tried to write it as defensively as I know how, but
like I said, I'm just learning this stuff myself.  Hope it's OK!   :)

You should be able to generalize this to more than one "set" of checkboxes,
but if not, let me know and I'll whip up another sample program.  Basically,
all the magic is in the "name=" parameter in the <input> tag.  Use the same
name for things that related options.  Use different names for different
groups of checkboxes (but put them all within the same <form> tag.

Note: I just realized that I used a for loop.  I recall reading somewhere
that foreach is more efficient than for, so that may be one way to improve
this.  Anyhow, here it is.

HTH,
Pete




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"';
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>

<html>
<head>
	<title>Checkboxes With PHP Example</title>
</head>


<body>

<form action="<? echo $_SERVER['PHP_SELF'] ?>?action=posted" method="post">
	<input type="checkbox" name="mycheckbox[]" value="<? echo date("l"); ?>"/>Choice 0<br/>
	<input type="checkbox" name="mycheckbox[]" value="hello"/>Choice 1<br/>
	<input type="checkbox" name="mycheckbox[]" value="2.3827"/>Choice 2<br/>
	<p><input type="submit"></p>
</form>

<?

// This checks to see if the "submit" button was hit.
//
if ( ! isset($_REQUEST['action']) )
	exit(0);


// The checkbox group with name "foobar[]" can be accessed
// by $_POST['foobar'].  As always, $_POST is an array, so
// the thing that we assign $_POST to is also an array, which
// can be counted, indexed, foreach'ed, etc.
//
// If nothing was checked, the $_POST['mycheckbox'] array doesn't
// exist, which will print a warning if we set E_ALL on.  So let's
// check to see if it exists first, and if not, tell the user to
// check something.
//
if ( ! isset($_POST['mycheckbox']) )
	die("I'm going to sit back and wait until you check something!");



$mycheckbox = $_POST['mycheckbox'];

echo '<ul>';

for ($i=0; $i < count($mycheckbox); $i++)
{ 

	// This shows you how to access checkboxes in PHP.  Now that you have a
	// list of all the stuff that's been checked, you can use a whole bunch
	// of conditional statements to form your complex SQL.
	//
	echo "<li>mycheckbox[$i]: $mycheckbox[$i]</li>"; 

}

echo '</ul>';


?>

</body>
</html>

-- 
The mathematics of physics has become ever more abstract, rather than more
complicated.  The mind of God appears to be abstract but not complicated.
He also appears to like group theory.  --  Tony Zee's "Fearful Symmetry"

GPG Fingerprint: B9F1 6CF3 47C4 7CD8 D33E  70A9 A3B9 1945 67EA 951D

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux