简体   繁体   English

下拉列表不发送POST数据

[英]Drop-down list doesn't send POST data

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. 我的情况是,我有2个下拉列表,并且只有一个下拉列表中的数据将被发送到第二页。 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. 问题是,当我选择第一个下拉列表时,它不会将任何POST数据从下拉列表发送到第二页,而仅发送空白。 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. 我只需要一个下拉列表中的1个数据,但是我两个都需要起作用。 If I can only choose one list, I dont even need to make 2 drop-down list. 如果我只能选择一个列表,则什至不需要制作2个下拉列表。

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. 这是php代码,用于显示直到查询之前我如何接收POST数据。

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. 我需要确保两个下拉列表都可以将POST数据发送到另一个页面,而不仅仅是一个页面正在运行。 I hope you can understand my problem. 希望你能理解我的问题。

EDIT: I have change both select box's names. 编辑:我已经更改了两个选择框的名称。 2nd EDIT: I add Fred's code 第二编辑:我添加弗雷德的代码

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. 因此,每个isset()都将始终返回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 . 现在的问题是,两个if(isset子句都将为true

What has already been said in regards to both selects holding the same name, still stands. 关于两个具有相同名称的选择,已经说过了,仍然成立。

Sidenote: (I renamed the 2nd select to time2 ) 旁注:(我将第二个选择重命名为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. 但是,为了使用单选按钮/下拉菜单选择中的时间,您需要使用isset条件语句,然后将该变量用于数据库插入。

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. 场景:将(设置)然后传递给数据库的$time变量设置为照此设置,具体取决于选择的选择时间。

<?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): 在您的情况下,它将是:(并且为此将您的提交按钮name="submit"name="submit" ):

<?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."'");

    }
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM