greencoin Posted June 20, 2007 Share Posted June 20, 2007 I've gone insane and now want to add to my online form the ability to search orders by the US state the company is in. I've created my form page which has 50 different checkboxes where the user can check as many boxes as they want. When the submit button is pressed the string is posted to the action page. The action page then collects the information passed and queries MySQL for the right rows. Wait a min, as I was typing I think I may have the solution which happens to be the code from my delete form! I'm thinking something like... <?php $count=mysql_num_rows($result); $submit = $_POST['submit']; $checkbox = $_POST['checkbox']; <input name="checkbox[]" type="checkbox" id="checkbox[]" value="AK"> <input name="submit" type="submit" id="submit" value="submit"> if($submit){ for($i=0;$i<$count;$i++){ $check_id = $checkbox[$i]; // <---- this needs to be modified I'm sure $sql = "SELECT FROM $tbl_name WHERE area='$check_id'"; $result = mysql_query($sql); ?> What do you think? Any tips on modifiying it is definitely appreciated. Thanks again ~Rich Quote Link to comment https://forums.phpfreaks.com/topic/56385-us-states-multiple-checkbox-query-will-this-work-is-there-an-easier-way/ Share on other sites More sharing options...
greencoin Posted June 20, 2007 Author Share Posted June 20, 2007 Originally I found this code online and was gonna use it... <?php $nr_options = count($_POST['options']); //(or $_GET, depending on what you use) // next query the DB with a loop for ($i=0; $i<$nr_options; $i++){ //while $i < nr of times the box was checked..perform a query $string= $_POST['options'][$i]; //this is the string you are looking for at the current position $query= 'SELECT * FROM Pets'. " WHERE description LIKE '%".$string."%'". $result = mysql_query($query) or die ("Error in query: $query " . mysql_error()); //rest of the actions you wanne perform would probably go here too...in the loop...like echo statements etc } ?> It seems to be the same with the exception of $string= $_POST['options'][$i]; ... Is the placement of the [$i] the part I'm missing? Quote Link to comment https://forums.phpfreaks.com/topic/56385-us-states-multiple-checkbox-query-will-this-work-is-there-an-easier-way/#findComment-278499 Share on other sites More sharing options...
greencoin Posted June 20, 2007 Author Share Posted June 20, 2007 ~bump~ Quote Link to comment https://forums.phpfreaks.com/topic/56385-us-states-multiple-checkbox-query-will-this-work-is-there-an-easier-way/#findComment-278660 Share on other sites More sharing options...
greencoin Posted June 21, 2007 Author Share Posted June 21, 2007 Nobody? I'm kinda shocked! lol ~Rich Quote Link to comment https://forums.phpfreaks.com/topic/56385-us-states-multiple-checkbox-query-will-this-work-is-there-an-easier-way/#findComment-279244 Share on other sites More sharing options...
aniesh82 Posted June 21, 2007 Share Posted June 21, 2007 I do not get exactly your requirement. To get the value of checkbox which are selected by the user use, if($_REQUEST['submit']) { if(count($_REQUEST['checkbox']) > 0) { $check_id = implode(",",$_REQUEST['checkbox']); $sql = "SELECT FROM $tbl_name WHERE area IN '$check_id' "; $result = mysql_query($sql); } } Quote Link to comment https://forums.phpfreaks.com/topic/56385-us-states-multiple-checkbox-query-will-this-work-is-there-an-easier-way/#findComment-279255 Share on other sites More sharing options...
greencoin Posted June 21, 2007 Author Share Posted June 21, 2007 I do not get exactly your requirement. Maybe that's why nobody gave an answer.. :-\ I have 50 checkboxes on my form page. I need the user to check as many boxes as they like, query the database, then display all the records that match the user's selections. <form method="POST" action="track_area_results.php"> Select the states to search <table width="500" border="0" cellspacing="0" cellpadding="0"> <tr> <td><input type="checkbox" name="state[]" value="ak"> AK</td> <td><input type="checkbox" name="state[]" value="hi"> HI</td> <td><input type="checkbox" name="state[]" value="me"> ME</td> <td><input type="checkbox" name="state[]" value="nj"> NJ</td> <td><input type="checkbox" name="state[]" value="sd"> SD</td> </tr> // continues for 45 more checkboxes so if HI, ME and SD are checked then the form will fetch all the records that match those fields. Thanks for replying aniesh82! ~Rich Quote Link to comment https://forums.phpfreaks.com/topic/56385-us-states-multiple-checkbox-query-will-this-work-is-there-an-easier-way/#findComment-279260 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.