I’ve got some problem in making a search using checkbox.
Basically, I’ve got some data that has its date of registration, i need to make search by months and retrieve the data according to selected month or months.
I am using php and mysql.
Here I have a simple html:
<ul>
<li><input type="checkbox" name="month_in" value="1" />January</li>
<li><input type="checkbox" name="month_in" value="2" />February</li>
<li><input type="checkbox" name="month_in" value="3" />March</li>
<li><input type="checkbox" name="month_in" value="4" />April</li>
<li><input type="checkbox" name="month_in" value="5" />May</li>
<li><input type="checkbox" name="month_in" value="6" />June</li>
<li><input type="checkbox" name="month_in" value="7" />July</li>
<li><input type="checkbox" name="month_in" value="8" />August</li>
<li><input type="checkbox" name="month_in" value="9" />September</li>
<li><input type="checkbox" name="month_in" value="10" />October</li>
<li><input type="checkbox" name="month_in" value="11" />November</li>
<li><input type="checkbox" name="month_in" value="12" />December</li>
<li><input type="submit" value="Search"></li>
</ul>
and part of php+mysql code:
$search=$_POST["month_in"];
$qry=mysql_query("select * from table where MONTH(dor) like '%{$search}%'");
The problem is that above code works only if user checks only one month, but if user checks 2 or more months it retrieves information only for the highest month (i mean if user has checked January, March, April and August it gives the results of August only).
The thing is that even if a user selects more than one checkbox, the value of the last checkbox selected will be sent back to your php script. If you want to post more than one value in array format, then change the name attribute of your checkboxes from
name="month_in"toname="month_in[]". So you will receive a numeric array of months.