如何使用checkbox在php中显示表中的数据


how to display data from a table in php using checkbox?

我的数据库中有多个主题,我的查询是通过在php中选择check boxes来获取这些主题的公共信息?假设有四个主题'a', 'b', 'c' and 'd',我也有单独的表,其中包含公共数据,即a intersect b, a intersect c and a intersect d的表等等

现在我尝试的是,首先用户必须从下拉框中选择主题,然后使用复选框选择多个比较主题。当我检查两个主题时,它只显示最后一个主题的数据。

<form method="GET" action="abc.php" name="form">
   <font size = "4px", color = "#008C9A", align = "left"><b> Select Theme >> </b></font>    
   <select name ="table[]">
     <option value="ab">a</option>
     <option value="bc">b</option>
     <option value="cd">c</option>
   <br/>
   <font size="4px" color="#008C9A" align="centre">
    <b> Show Common Genes of Selected Theme with </b>
   </font> <br/> <br/>
   <input type="checkbox" name="search" value = "a"/> 
    <font color = "#2F4F4F"><b>a </b></font>                         
   <input type="checkbox" name="search" value = "a"/>
    <font color = "#2F4F4F"><b>b</b></font>                       
   <input type="checkbox" name="search"/ value = "c"> 
    <font color = "#2F4F4F"><b>c</b></font>                         
   <input type= "submit" value= "Search" name="sub" /> 
</form>

有人能建议我什么应该是我的action.php吗?

我正在尝试这个,但没有结果:

    <?php
    require("config.php");
    $table=$_POST['table'];
    $search=$_POST['search'];
  echo"<table border='0', width='1200', align='center'>
<tr>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>Theme</font></th>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>Id</font></th>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>Symbol</font></th>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>Name</font></th>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>Synonyms</font></th>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>Chromosome</font></th>
<th bgcolor = '#2F4F4F' align = center><font color = '#fff'>New_Id</font></th>
</tr>";
        foreach($search as $key)
        {
switch($key)
            {
      case"cancer":         
   $sql = mysql_query("select * from $table where theme = 'cancer'", $con);
    $num_row=mysql_num_rows($sql);

    for($i=0;$i<$num_row;$i++)
{
$result=mysql_fetch_assoc($sql);

  echo"<tr bgcolor = '#EBDDE2'>";
  echo"<td align=center><font color = 'purple'><b>" . $result['theme'] ."</b></font></td>";
    $strName2 = $result["Locus_id"];
   echo"<td align=center><font color = 'purple'><b>" . $result['id'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['symbol'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['name'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Synonyms'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Chromosome'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['new_id'] ."</b></font></td>";
  echo"</tr>";
}
    break;          
    case"diabetes":         
   $sql = mysql_query("select * from $table where theme = 'diabetes'", $con);
    $num_row=mysql_num_rows($sql);

    for($i=0;$i<$num_row;$i++)
{
$result=mysql_fetch_assoc($sql);
echo"<tr bgcolor = '#EBDDE2'>";
  echo"<td align=center><font color = 'purple'><b>" . $result['theme'] ."</b></font></td>";
    $strName2 = $result["Locus_id"];
   echo"<td align=center><font color = 'purple'><b>" . $result['id'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['symbol'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['name'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Synonyms'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Chromosome'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['new_id'] ."</b></font></td>";
  echo"</tr>";
}
}
    break;          
            case"tubersulosis":         
   $sql = mysql_query("select * from $table where theme = 'tuberculosis'", $con);
    $num_row=mysql_num_rows($sql);

    for($i=0;$i<$num_row;$i++)
{
$result=mysql_fetch_assoc($sql);
echo"<tr bgcolor = '#EBDDE2'>";
  echo"<td align=center><font color = 'purple'><b>" . $result['theme'] ."</b></font></td>";
    $strName2 = $result["Locus_id"];
   echo"<td align=center><font color = 'purple'><b>" . $result['id'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['symbol'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['name'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Synonyms'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Chromosome'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['new_id'] ."</b></font></td>";
  echo"</tr>";
}
}
    break;
            case"hiv":          
   $sql = mysql_query("select * from $table where theme = 'HIV'", $con);
    $num_row=mysql_num_rows($sql);

    for($i=0;$i<$num_row;$i++)
{
$result=mysql_fetch_assoc($sql);
echo"<tr bgcolor = '#EBDDE2'>";
  echo"<td align=center><font color = 'purple'><b>" . $result['theme'] ."</b></font></td>";
    $strName2 = $result["Locus_id"];
   echo"<td align=center><font color = 'purple'><b>" . $result['id'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['symbol'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['name'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Synonyms'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['Chromosome'] ."</b></font></td>";
  echo"<td align=center><font color = 'purple'><b>" . $result['new_id'] ."</b></font></td>";
  echo"</tr>";
}
}
    break;
        }
            }
mysql_close($con);
    ?>

根据@manoj的建议,将名称更改为search[],而不是search。并对CCD_ 6进行了形式化处理。

现在在abc.php中,您可以通过获取复选框值

$search = $_POST['search'];//this will be an array

剩下的我想你可以做,对吧?

您的表单代码应该是:

<form method="POST" action="abc.php" name="form"> <!-- You can keep GET as well -->
   <font size="4px" color="#008C9A" align="left"><!-- no impact but extra , removed-->
    <b> Select Theme >> </b>
   </font>    
   <select name="table"> <!-- why using [] if multiple option can't be selected -->
    <option value="ab">a</option>
    <option value="bc">b</option>
    <option value="cd">c</option>
   </select> <!-- Important: missing close select tag -->
   <br/>
   <font size="4px" color="#008C9A" align="center">
    <b> Show Common Genes of Selected Theme with </b>
   </font> <br/> <br/>
   <input type="checkbox" name="search[]" value="a" /> <!-- name should use [] notation if multiple values -->
    <font color = "#2F4F4F"><b>a </b></font>                         
   <input type="checkbox" name="search[]" value="b" /> <!-- Note value change here -->
    <font color="#2F4F4F"><b>b</b></font>                       
   <input type="checkbox" name="search[]" value="c" /> <!-- extra / removed -->
    <font color="#2F4F4F"><b>c</b></font>                         
   <input type="submit" value="Search" name="sub" /> 
</form>

在您的PHP文件中,您可以访问值:

$search = $_POST['search']; // checkbox values
print_r($search);
$table = $_POST['table']; // select value
echo $table;