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> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用