-1

In the php script below, I got a checkbox ('solstices') and a pair of radiobuttons which happen to be arrays as well. I want them to be active if the checkbox is checked(it works). So if not checked it operates the last query. However I have a problem with operating these radiobuttons. Only the "Winter Solstice" is working right now no matter if I choose the "Summer Solstice". How can I declare their values sufficiently.

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\""; 
};
Lef_Chef
  • 19
  • 1
  • 8
  • Can you please update the code with the value of "$sql_columns" ? You should also display the $_POST values using `print_r($_POST);`. Otherwise, we won't be able to help you. – Wissam El-Kik May 27 '14 at 08:53
  • http://stackoverflow.com/questions/22922796/retrieve-data-from-sql-database-and-display-in-tables-display-certain-data-acc – Lef_Chef May 27 '14 at 09:01
  • Here you can find further information about my project and it also contains all my php script. – Lef_Chef May 27 '14 at 09:02
  • The following variable (`$sql_columns`) isn't mentioned in the PHP script. Please provide the code so we can help you. – Wissam El-Kik May 27 '14 at 09:19
  • check again. I have provided the whole script and ofcourse the $sql_columns – Lef_Chef May 27 '14 at 09:22

3 Answers3

0

You're overwriting the check_sol[] variable so it always just contains 2. Adding [] in the ID doesn't make it an array. You need to give both inputs different IDs and construct the array on the PHP side if you really need one.

baarkerlounger
  • 1,217
  • 8
  • 40
  • 57
  • If I give a different name to the radiobuttons then they won't be a group anymore. The result is that they could be checked both of them at the same time and I don't want this to happen. – Lef_Chef May 27 '14 at 08:24
0

Please try this like...

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\""; 
}
0

Removed the brackets [] from html code(check_sol[]), propably it is not suitable for radiobuttons. Plus a few noticeable changes in php code.

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\""; 
};
Lef_Chef
  • 19
  • 1
  • 8