dongren1986 2012-09-18 11:51
浏览 7
已采纳

在PHP中使用复选框值

I have this page (code below) which contains a series of 5 checkboxes, all representing different values, and i need to use them to query a database.

<form id="form1" name="form1" method="post" align="center" action="section_search_results1.php">
      <p>Select The <b>Section</b> You Wish To Search In Below...      </p>
      <p>
        <label for="section"></label>
        <label for="section2"></label>
        <label>
          <input type="checkbox" name="SectionSelect" value="Functional" id="SectionSelect_0" />
          Functional</label>
        <label>
          <input type="checkbox" name="SectionSelect" value="Technical" id="SectionSelect_1" />
        Technical</label>
        <label>
          <input type="checkbox" name="SectionSelect" value="Commercial" id="SectionSelect_2" />
        Commercial</label>
        <label>
          <input type="checkbox" name="SectionSelect" value="Implementation" id="SectionSelect_3" />
        Implementation</label>
        <label>
          <input type="checkbox" name="SectionSelect" value="Innovation" id="SectionSelect_4" />
        Innovation</label>
        <br />
      </p>
      <p>Enter the <b>Keyword(s) or Keyphrase</b> Below...</p>
      <p>
        <label for="kword"></label>
        <input type="text" name="kword" id="kword" placeholder="Enter Keyword(s)" />
        <br />
      </p>
      <p>
        <input type="submit" name="submit" id="submit" value="Search" /> | | <input type="reset" name="clear" id="clear" value="Clear" />
      </p>
    </form>

as you can see each has its own value which is the term used to query the database. the php query code on the results page is as follows

    <?php
          $kword = $_POST["kword"];
          $section = $_POST["SectionSelect"];

          function boldText($text, $kword) {
        return str_ireplace($kword, "<strong>$kword</strong>", $text);
    }


       // Connects to your Database 
     mysql_connect("localhost", "root") or die(mysql_error()); 
     mysql_select_db("test") or die(mysql_error()); 
     mysql_real_escape_string($kword); 
     $data = mysql_query("select company_name, section_name, question, answer from company, rfp, section, question_keywords
    where company.company_id = rfp.company_id
    and rfp.rfp_id = question_keywords.rfp_id
    and question_keywords.section_id = section.section_id
    and section_name like '%$section%'
    and keywords like '%$kword%';") 
     or die(mysql_error());
?>

ultimately what i want this to do is to query the database with the query to potentially have where clauses for each of the checkbox values? for example i want to select x where y like 'Technical' AND y like 'Functional' and so on...

Any help would be great

thanks

  • 写回答

1条回答 默认 最新

  • dongzhan8001 2012-09-18 11:55
    关注

    Set up your checkbox names as array-keys to use them as an array with values of all selected checkboxes:

    <input type="checkbox" name="SectionSelect[0]" value="Functional" />
    <input type="checkbox" name="SectionSelect[1]" value="Technical" />
    
    echo $_POST['SectionSelect'][0]; // should print "Functional"
    echo $_POST['SectionSelect'][1]; // should print "Technical"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。