如何在php中发布所选值

时间:2014-05-27 07:57:59

标签: php radio-button radio-group

在下面的php脚本中,我得到了一个复选框('solstices')和一对恰好是数组的radiobutton。如果选中复选框,它希望它们处于活动状态(它可以工作)。因此,如果没有选中,它将操作最后一个查询但是我操作这些无线电按钮有问题。无论我选择“夏至”,现在只有“冬至”正在运作。我怎样才能充分宣布他们的价值观。

HTML:

    <tr><td colspan="10" align="center"><h2>Solstices</h2></td></tr>
  <tr><td><input name="solstices" type="checkbox" id="solstices" value="1" />Solstices<br /></td></tr>
 <td><input name="check_sol[]" type="radio" id="check_sol[]" value="1" />Summer Solstice</td>
 <td><input name="check_sol[]" type="radio" id="check_sol[]" value="2" />Winter Solstice<br /></td></tr>

PHP:

//DNI CHECKBOX + ALL

if(isset($_POST['solstices'])){
        if(isset($_POST['check_sol'])=='1'){
$tmp="SELECT DISTINCT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE=\"2012-06-11\"";
        }
        if(isset($_POST['check_sol'])=='2'){ 
$tmp="SELECT DISTINCT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE= \"2011-12-21\"";
        }
}
else {
$tmp ="SELECT DISTINCT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\""; 
};

3 个答案:

答案 0 :(得分:0)

您正在覆盖check_sol []变量,因此它始终只包含2.在ID中添加[]并不会使它成为一个数组。如果确实需要,则需要为两个输入提供不同的ID并在PHP端构造数组。

答案 1 :(得分:0)

请尝试这样......

if(isset($_POST['solstices']))
{
    if(isset($_POST['check_sol'][0])=='1')
    {
    $tmp="SELECT DISTINCT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE=\"2012-06-11\"";
    }
    if(isset($_POST['check_sol'][0])=='2') 
    {
    $tmp="SELECT DISTINCT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE= \"2011-12-21\"";
    }
}
else
{
    $tmp ="SELECT DISTINCT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\""; 
}

答案 2 :(得分:0)

从html代码(check_sol [])中删除了括号[],可能它不适用于radiobuttons。加上PHP代码中的一些明显变化。

HTML:

<tr><td><input name="solstices" type="checkbox" id="solstices" value="1" />Solstices</td></tr>
 <td><input name="check_sol" type="radio"  value="8" checked="checked" />Summer Solstice</td>
 <td><input name="check_sol" type="radio"  value="9" />Winter Solstice  <br /></td></tr>

PHP:

if(isset($_POST['solstices'])){
        if($_POST['check_sol']=='8'){
        $tmp="SELECT DISTINCT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE=\"2012-06-11\"";
        }
        else { 
        $tmp="SELECT DISTINCT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE= \"2011-12-21\"";
        }
}
else {
$tmp ="SELECT DISTINCT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\""; 
};