2013-04-08 117 views
0

在下面的代碼,我試圖來處理選擇選項或文本字段的值:處理選擇選項在PHP中的許多下拉菜單的情況下

<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'; 
} 
} 
?> 

正如你可以在看代碼,當用戶選擇操作號時,會出現文本字段,然後用戶將輸入操作號以顯示此維護操作的類型。類似地,當用戶選擇操作類型時,將顯示維護類型列表,以便用戶選擇他需要的操作類型等等,以用於剩餘的選擇選項。

我面臨的問題是處理用戶選擇或輸入的內容(在通過操作否搜索的情況下)。正如你在代碼中看到的那樣,我使用了if (isset($_POST['formsubmitted'])){,但它不起作用。任何建議傢伙。

回答

1

讓我們來看看你的例子:

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> 

然後最後得到TPE值從$_POST

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

的formsubmitted名稱是從您的輸入中缺少提交:

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

這應該工作。

相關問題