Forum Moderators: coopster

Message Too Old, No Replies

php mysql search with checkboxes help!

         

tkhameleonu085

6:47 pm on Jul 1, 2009 (gmt 0)

10+ Year Member



I am trying to build a page where a user can select the checkboxes and when the submit button is pressed it will query the database based on the checkboxes selected. I have the checkboxes and submit button but I dont know where to start on the php scripting, I am very new to php can someone help please!

rocknbil

8:31 pm on Jul 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard tkhameleonu085, a checkbox will be present in $_POST/$_GET/$_REQUEST, or not, depending on if it's checked or not. so

$var chks = Array('chk1','chk2','chk3');

for each ($chk as $checked) {
if (isset($_POST[$checked])) {
// <input type="checkbox" name="$check"> was checked
}
}

andrewsmd

6:34 pm on Jul 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll break it down a little further for you. Paste this into a temp file and play with it a bit. Of course you could put your checkbox names in an array as rocknbil did, but since you said you were new, I thought I would give you a basic approach to it.

<html>
<form method = "post" name = "form">
<input type ="checkbox" name = "chk1" /> Checkbox 1 <br>
<input type ="checkbox" name = "chk2" /> Checkbox 2 <br>
<input type ="checkbox" name = "chk3" /> Checkbox 3 <br>
<input type ="submit" name = "submitName" /><br>
</form>
<?php

//if they clicked the submit button
if(isset($_POST['submitName'])){

//if they checked box 1
if(isset($_POST['chk1'])){

echo("You checked box 1<br>");

}//if isset chk1

//if they checked box 2
if(isset($_POST['chk2'])){

echo("You checked box 2<br>");

}//if isset chk2

//if they checked box 3
if(isset($_POST['chk3'])){

echo("You checked box 3<br>");

}//if isset chk3

}//if isset
?>

</html>

tkhameleonu085

7:26 pm on Jul 2, 2009 (gmt 0)

10+ Year Member



thank you guys both i appreciate the help, but now how do I get the mysql database to query the table based on the checked boxes