doushi4795 2015-05-14 16:18
浏览 122
已采纳

使用多个复选框返回MySQL结果

I am trying to create a search function that allows the user to choose multiple categories, select submit, and then the php will return the mysql results that match the selected categories.

I would like to do this without AJAX or jQuery, but I will use those if required. I am newer to the world of PHP, so I think I'm on the right track, but would love some assistance of getting this correct.

HTML

<form action="filter.php" method="post">
    <p class="searching">Filter by category</p>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Arts/Culture"/><img class="lilIcon" src="img/anc.png"><h5>Arts/Culture</h5></section>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Seniors"/><img class="lilIcon" src="img/senior.png"><h5>Seniors</h5></section>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Youth"/><img class="lilIcon" src="img/youth.png"><h5>Youth</h5></section>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Animals"/><img class="lilIcon" src="img/pet.png"><h5>Animals</h5></section>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Community"/><img class="lilIcon" src="img/comm.png"><h5>Community</h5></section> 
    <section class="box"><input type="checkbox" name="fill" class="check" value="Crisis Support"/><img class="lilIcon" src="img/cs.png"><h5>Crisis Support</h5></section>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Environment"/><img class="lilIcon" src="img/leaf.png"><h5>Environment</h5></section>
    <section class="box"><input type="checkbox" name="fill" class="check" value="Faith Based"/><img class="lilIcon" src="img/pray.png"><h5>Faith Based</h5></section> 
    <section class="box"><input type="checkbox" name="fill" class="check" value="People with Disabilities"/><img class="lilIcon" src="img/chair.png"><h5>People with Disabilities</h5></section>
    <input type="submit" name="submit" id="filter" value="Filter"/>    
</form> 

PHP

<?php
$variable=$_POST['fill'];
$i = 0;
foreach ($variable as $variablename) {
    $i++;
    $variablename[number];
}

$result = "SELECT * FROM opportunity WHERE `Category`=$variablename";
$num_rows = mysql_num_rows($result);
   echo "<div class='holder'>"; 

   if(mysql_num_rows($result) > 0)
   {
       while($results = mysql_fetch_array($result));
   }  
</div>
  • 写回答

1条回答 默认 最新

  • douhui3305 2015-05-14 16:53
    关注

    I changed the value of the submit to 1

    <input type="submit" name="submit" id="filter" value="1"/>   
    
    if (intval($_POST['submit') == 1){
      $count = 0;
      $sql = "SELECT * FROM opportunity WHERE `Category` IN ( ";
      $chk = $_POST['chk'];
      foreach($chk as $value){
        $sql .= "'$value',"
        $count++;
      }
      if ($count > 0){
        $sql = substr($sql,0,-1) . ');';  // remove trailing comma and close
    
         // your sql and output go here
    
      }
    }
    

    If Youth and Animals were selected $sql would be:

    SELECT * FROM opportunity WHERE `Category` IN ('Youth','Animals');
    

    HTML

    <form action="filter.php" method="post">
    <p class="searching">Filter by category</p>
    <section class="box"><input id="chk1" type="checkbox" name="chk[]" class="check" value="Arts/Culture"/><img class="lilIcon" src="img/anc.png"><h5>Arts/Culture</h5></section>
    <section class="box"><input id="chk2" type="checkbox" name="chk[]" class="check" value="Seniors"/><img class="lilIcon" src="img/senior.png"><h5>Seniors</h5></section>
    <section class="box"><input id="chk3" type="checkbox" name="chk[]" class="check" value="Youth"/><img class="lilIcon" src="img/youth.png"><h5>Youth</h5></section>
    <section class="box"><input id="chk4" type="checkbox" name="chk[]" class="check" value="Animals"/><img class="lilIcon" src="img/pet.png"><h5>Animals</h5></section>
    <section class="box"><input id="chk5" type="checkbox" name="chk[]" class="check" value="Community"/><img class="lilIcon" src="img/comm.png"><h5>Community</h5></section> 
    <section class="box"><input id="chk6" type="checkbox" name="chk[]" class="check" value="Crisis Support"/><img class="lilIcon" src="img/cs.png"><h5>Crisis Support</h5></section>
    <section class="box"><input id="chk7" type="checkbox" name="chk[]" class="check" value="Environment"/><img class="lilIcon" src="img/leaf.png"><h5>Environment</h5></section>
    <section class="box"><input id="chk8" type="checkbox" name="chk[]" class="check" value="Faith Based"/><img class="lilIcon" src="img/pray.png"><h5>Faith Based</h5></section> 
    <section class="box"><input id="chk9" type="checkbox" name="chk[]" class="check" value="People with Disabilities"/><img class="lilIcon" src="img/chair.png"><h5>People with Disabilities</h5></section>
    <input type="submit" name="submit" id="filter" value="1"/>    
    </form> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题