Jump to content

US states, multiple checkbox query - will this work? Is there an easier way?


Recommended Posts

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

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?

 

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);

}

}

 

 

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.