繁体   English   中英

处理如果有许多下拉菜单,请在PHP中选择选项

[英]Handling Select option in PHP in case of many drop-down menu

在下面的代码中,我试图处理选择选项或文本字段的值:

<script>
function check() {
    var dropdown = document.getElementById("OpType");
    var current_value = dropdown.options[dropdown.selectedIndex].value;

    if (current_value == "OpNo") {
        document.getElementById("operationno").style.display = "block";
        document.getElementById("MainType").style.display = "none";
        document.getElementById("MemName").style.display = "none";
        document.getElementById("EmpName").style.display = "none";
        <?php $TPE = '1'?>
    }
    else if (current_value == "OpTyp") {
        document.getElementById("MainType").style.display = "block";
        document.getElementById("MemName").style.display = "none";
        document.getElementById("EmpName").style.display = "none";
        document.getElementById("operationno").style.display = "none";
        <?php $TPE = '2'?>
    }
    else if (current_value == "OpMem") {
        document.getElementById("MemName").style.display = "block";
        document.getElementById("operationno").style.display = "none";
        document.getElementById("MainType").style.display = "none";
        document.getElementById("EmpName").style.display = "none";
        <?php $TPE = '3'?>
    }
    else if (current_value == "OpEmp"){
        document.getElementById("MemName").style.display = "none";
        document.getElementById("operationno").style.display = "none";
        document.getElementById("MainType").style.display = "none";
        document.getElementById("EmpName").style.display = "block";
        <?php $TPE = '4'?>
    }
    else if (current_value == "blank") {
        document.getElementById("MainType").style.display = "none";
        document.getElementById("MemName").style.display = "none";
        document.getElementById("EmpName").style.display = "none";
        document.getElementById("operationno").style.display = "none";
        <?php $TPE = '0'?>
    }
}
</script>

<form name="f1" action="FollowOperations.php" method="post">
<select id="OpType" onChange="check();">
<option value="blank">Choose</option>
<option value="OpNo">Operation No</option>
<option value="OpTyp">Operation Type</option>
<option value="OpMem">Maintenance Member</option>
<option value="OpEmp">Employee</option>
</select><br>

<input class="tb10" type="text" id="operationno" size="4" style="text-align: center" style="display: none">
                         
<select id="MainType" style="display: none">
<option value="blank">Choose</option>
<option value="printing">Printing</option>
<option value="maintenance">PC Maintenance</option>
<option value="internet">Internet Problem</option>
<option value="software">Software</option>
<option value="email">Email Problem</option>
<option value="usbcd">USB/CD Problem</option>
</select>

<select id="MemName" style="display: none">
<option value="blank">Choose</option>
<option value="john">John</option>
<option value="hen">Hen</option>
</select>
                         
<select id="EmpName" style="display: none">
<option value="blank">Choose</option>
<option value="smith">Smith</option>
<option value="will">William</option>
<option value="Gor">George</option>
</select>

<input type="submit" value="Submit" class="button" /> 
</form>

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

if ($TPE == '1'){

$operationno = $_POST['operationno'];
$query_retrieve_maintenance = "Select Type from Maintenance where ID = '$operationno'";
$result_retrieve_maintenance = mysqli_query($dbh, $query_retrieve_maintenance);
$maintenance_type = mysqli_fetch_row($result_retrieve_maintenance);
$maintenance_typet = $maintenance_type[0];
echo $maintenance_typet;

} else if ($TPE == '2'){

$MainType = $_POST['MainType'];
if ($MainType=='email'){echo 'Email is not ava';}

} else if ($TPE == '3'){

$MemName = $_POST['MemName'];

} else if ($TPE == '4'){

$EmpName = $_POST['EmpName'];

} else{

$msg = 'not available';
}
}
?>

正如您在代码中看到的,当用户选择操作号时,将显示一个文本字段,然后用户将输入操作号以显示此维护操作的类型。 类似地,当用户选择操作类型时,将显示维护类型列表,以便用户选择他需要的操作类型,以此类推剩余的选择选项。

我面临的问题是处理用户选择或输入的内容(在通过操作no搜索的情况下)。 正如您在代码中看到的,我使用了if (isset($_POST['formsubmitted'])){但它没有用。 任何建议的家伙。

输入提交中缺少表单提交的名称:

<input type="submit" value="Submit" class="button" name="formsubmitted" /> 

这应该工作。

让我们看看你的例子:

if (current_value == "OpNo") {
    document.getElementById("operationno").style.display = "block";
    document.getElementById("MainType").style.display = "none";
    document.getElementById("MemName").style.display = "none";
    document.getElementById("EmpName").style.display = "none";
    <?php $TPE = '1'?>
}

您将PHP代码粘贴到javascript中。 <?php $TPE = '1'?>和所有其他语句都将由PHP执行,而不是取决于javascript的条件。

你可以执行序列:

If something happened in PHP (on server) -> then execute some code of javascript (on client)因为您将能够决定JS代码客户端应该执行什么。

但不能:

If something happened in Javascript(client) -> execute code of PHP(server)除非您将xmlhttp请求发送到网页。

因此,在您的情况下,您的$TPE将始终为0

我建议的是:1。将您的数据放入数组进行映射,然后您可以改进它:

$data = array(
  "OpNo" => 1,
  "OpTye" => 2,
  "OpMem" => 3,
  "OpEmp" => 4,
  "blank" => 5
);

然后在表单中创建您的选择,创建属性name

<select id="OpType" name="tpe" onChange="check();">
<option value="blank">Choose</option>
<option value="OpNo">Operation No</option>
<option value="OpTyp">Operation Type</option>
<option value="OpMem">Maintenance Member</option>
<option value="OpEmp">Employee</option>
</select><br>

最后,从$_POST获取TPE值

<?php
    if (isset($_POST['tpe'])){
      if ((int) $_POST['tpe'] === $data['OpNo']){
        //Do your stuff.
      ?>
        <script>
          executeSomeJSFunctionsHere();
        </script>
      <?php
      }
    }
?>

暂无
暂无

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

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