Domanda

My situation is, I have 2 drop-down list and only one data from one of the drop-down list will be sent into the second page. That's mean, the data from another drop-down list will not be sent. So, I choose to display the drop-down list by using the radio button. If I choose the 1st button, it will only display the 1st drop-down list and vice versa.

The problem is, when I choose the 1st drop-down list, it doesn't send any POST data from the drop-down list to the second page, only blank. But, if I choose the 2nd drop-down list, it send the data properly! I thought that there are errors in 1st drop-down list codes (even both codes are practically identical). So, I add another list, and this time, only the 3rd drop-down list's data is sent. 1st and 2nd list doesn't send anything.

I realize that my problem is my codes only sent data from the last drop-down list, not both. I only need 1 data from either drop-down list, but I need both to function. If I can only choose one list, I dont even need to make 2 drop-down list.

This is my codes, but not a full code. The other data works fine, only the drop-down list is having problem.

<form name="list" action="index.php?site=11" method="post">
<script>
function check(){
if(document.getElementById('1H').checked) {
    document.getElementById('D1H').style.display = 'block';
    document.getElementById('D2H').style.display = 'none';
}
else {
    document.getElementById('D1H').style.display = 'none';
    document.getElementById('D2H').style.display = 'block';
}}
</script>
Choose:
<input type="radio" onclick="javascript:check();" name="duration" id="1H">1 Hour
<input type="radio" onclick="javascript:check();" name="duration" id="2H">2 Hour

Choose time slot:
<div id="D1H" style="display:none">
    <select name="time1" >
     <option value="">---Choose---</option>
     <option value="8-9">8:00am-9:00am</option>
    </select>
</div>
<div id="D2H" style="display:none">
    <select name="time2" >
     <option value="">---Choose---</option>
     <option value="8-10">8:00am-10:00am</option>
    </select>
</div>
<input type="submit" name="submit" value="Next">

This is php codes to show how I receive the POST data, just until the query.

include('../include/dbconnect.php');
$user = $_SESSION['username'];

if(isset($_POST['submit'])){

if(isset($_POST['time'])){
$lab=$_POST['lab'];
$day=$_POST['day'];
$month=$_POST['month'];
$year=$_POST['year'];
$time=$_POST['time'];

echo "1:".$time;

$results = mysql_query("SELECT COUNT(*) FROM `booking` WHERE `lab` = '".$lab."' AND `day` = '".$day."' AND `month` = '".$month."' AND `year` = '".$year."' AND `time` = '".$time."'");
}

if(isset($_POST['time2'])){
$lab=$_POST['lab'];
$day=$_POST['day'];
$month=$_POST['month'];
$year=$_POST['year'];
$time=$_POST['time2'];

echo "2:".$time;

$results = mysql_query("SELECT COUNT(*) FROM `booking` WHERE `lab` = '".$lab."' AND `day` = '".$day."' AND `month` = '".$month."' AND `year` = '".$year."' AND `time` = '".$time."'");
}
}

echo "3:".$time;

How do I solve this problem? I need to make sure that both drop-down list can sent POST data to another pages, not only one functioning at all. I hope you can understand my problem.

EDIT: I have change both select box's names. 2nd EDIT: I add Fred's code

È stato utile?

Soluzione

The select boxes both have the same name. You need to set different name attributes for the two select options.

Edit

The issue is that the select box will post data even if it's not explicitly set. So each isset() will always return true.

As long as your default option has an empty value attribute you can check against that like this:

if(!empty($_POST['time']))

Or maybe set a default value to test against like so:

<option value="0">---Choose---</option>

<?php if($_POST['time'] != '0') ?>

The original issue was that the form would always post the last select even if you only made a choice with the first one. The issue now is that both if(isset clauses will be true.

Altri suggerimenti

What has already been said in regards to both selects holding the same name, still stands.

Sidenote: (I renamed the 2nd select to time2)

However, in order to use the time from either radio button/dropdown selects, you need to use an isset conditional statement, then use that variable for your DB insert.

Tested as follows while naming the submit button as my own self test and inside the same file. You can modify it to suit.

Scenario: The $time variable that's being (set) then passed to your DB, will be set as such, depending on which time select was chosen.

<?php
if(isset($_POST['submit'])){

    if(isset($_POST['time'])){
    $time=$_POST['time'];
    echo $time;
    }

    if(isset($_POST['time2'])){
    $time=$_POST['time2'];
    echo $time;
    }
}

?>

<form name="list" action="" method="post">
<script>
function check(){
if(document.getElementById('1H').checked) {
    document.getElementById('D1H').style.display = 'block';
    document.getElementById('D2H').style.display = 'none';
}
else {
    document.getElementById('D1H').style.display = 'none';
    document.getElementById('D2H').style.display = 'block';
}}
</script>
Choose:
<input type="radio" onclick="javascript:check();" name="duration" id="1H">1 Hour
<input type="radio" onclick="javascript:check();" name="duration" id="2H">2 Hour

Choose time slot:
<div id="D1H" style="display:none">
    <select name="time" >
     <option value="">---Choose---</option>
     <option value="8-9">8:00am-9:00am</option>
    </select>
</div>
<div id="D2H" style="display:none">
    <select name="time2" >
     <option value="">---Choose---</option>
     <option value="8-10">8:00am-10:00am</option>
    </select>
</div>
<input type="submit" name="submit" value="Next">

EDIT (DB-related)

And in your case, it would be: (and do name your submit button to name="submit" for this):

<?php
if(isset($_POST['submit'])){

    if(isset($_POST['time'])){
    $lab=$_POST['lab'];
    $day=$_POST['day'];
    $month=$_POST['month'];
    $year=$_POST['year'];
    $time=$_POST['time'];
    
    $results = mysql_query("SELECT COUNT(*) FROM `booking` WHERE `lab` = '".$lab."' AND `day` = '".$day."' AND `month` = '".$month."' AND `year` = '".$year."' AND `time` = '".$time."'");

    }

    if(isset($_POST['time2'])){
    $lab=$_POST['lab'];
    $day=$_POST['day'];
    $month=$_POST['month'];
    $year=$_POST['year'];
    $time=$_POST['time2'];
    $results = mysql_query("SELECT COUNT(*) FROM `booking` WHERE `lab` = '".$lab."' AND `day` = '".$day."' AND `month` = '".$month."' AND `year` = '".$year."' AND `time` = '".$time."'");

    }
}

?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top