2016-11-11 11 views
0

HTML Javascript PHPとMYSQLを使用して、以前の入力に依存する選択入力をHTMLフォームで作成しようとしています。これは私の前のページの1つと一緒に働いていたので、この新しいものを作成するときに、javascriptファイルに新しい関数を定義し、新しいPHPファイルを作成してデータを送信しました。フォーム。HTMLフォームの入力は、javascriptとphpで以前のフォーム入力に依存しています。

私は以前の関数とPHPファイルをわずかな変更だけで反映していましたが、最後にドロップダウンを選択したときに作成したオプションは表示されませんでした。実際には空白のPHPエラーのないページが通過しているのですが、なぜこれが起こっているのかわかりません。ここで

は私のフォームのスクリーンショットであり、あなたは、「ラックユニットの場所」オプションは、任意のオプションを作成していないと、ブランクyethある他の選択ボックスが正常に動作を確認できます。

Form Screenshot

これは、私のHTMLフォームのセクションでは、Rack Unit LocationはRack Numberに依存したいと思っています。これはLocationに依存し、Rackに依存します。

<tr> 
    <th>Rack </th> 
    <script src='includes/Jquery/default.js'></script> <!-- Link to new Javascript file --> 
    <td>  
     <select id='rack_name' name="rack_name" onchange="window.loadLocation()"> 
     <option disabled="disabled" selected="selected">Rack Name</option> 
     <?php 
      $r = mysqli_query($conn, "SHOW TABLES WHERE Tables_in_network Like 'rack%'"); 
      while($row=mysqli_fetch_assoc($r)){ 
       echo "<option value=".$row['Tables_in_network'].">".$row['Tables_in_network']."</option>"; 
      } 
     ?> 
     <option value="no_rack">Not in Rack</option> 
     </select> 
    </td> 
</tr> 
<tr> 
    <th>Location</th> 
    <td> 
     <select id="location" name="location" onchange="window.selectrack()"> 
     <option disabled="disabled" selected="selected">Location</option>       
     </select> 
    </td> 
</tr> 
<tr> 
    <th>Rack Number</th> 
    <td> 
     <select id="rackno" name="rackno" onchange="window.selectpduunit()"> 
     <option disabled="disabled" selected="selected">Rack Number</option>   
     </select> 
    </td> 
</tr> 
<tr> 
    <th>Rack Unit Location</th> 

    <td> 
     <select name="rackunit" id="rackunits"> 
     <option disabled="disabled" selected="selected">Rack Unit Number</option> 
     </select> 
    </td> 
</tr> 

これらのonchange関数は、次のJavaScriptファイルで定義されています。

function loadLocation(){  
var formName = 'switch'; 
var rackname = document[formName]['rack_name'].value; 

var xmlhttp = null; 
if(typeof XMLHttpRequest != 'udefined'){ 
    xmlhttp = new XMLHttpRequest(); 
}else if(typeof ActiveXObject != 'undefined'){ 
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
}else{ 
    throw new Error('You browser doesn\'t support ajax');   
} 
xmlhttp.open('GET', 'load_location.php?rack='+rackname, true); 
xmlhttp.onreadystatechange = function(){ 
    if(xmlhttp.readyState == 4) 
     window.insertLocation(xmlhttp); 
}; 
xmlhttp.send(null); 
} 

function insertLocation(xhr){ 
    if(xhr.status == 200){ 
    document.getElementById('location').innerHTML = xhr.responseText; 
    }else{ 
    throw new Error('Server has encountered an error\n'+'Error code = '+xhr.status); 
    } 
} 

function selectrack(){  
    var formName = 'switch'; 
    var rackname = document[formName]['rack_name'].value; 
    var location = document[formName]['location'].value; 
    var xmlhttp = null; 
    if(typeof XMLHttpRequest != 'undefined'){ 
     xmlhttp = new XMLHttpRequest(); 
    }else if(typeof ActiveXObject != 'undefined'){ 
     xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
    }else { 
     throw new Error('You browser doesn\'t support ajax');   
    } 
    xmlhttp.open('GET', 'select_rack_number.php?location='+location+'& rack='+rackname, true); 
     xmlhttp.onreadystatechange = function(){ 
     if(xmlhttp.readyState == 4) 
      window.insertrackno(xmlhttp); 
    }; 
    xmlhttp.send(null); 
} 

function insertrackno(xhr){ 
    if(xhr.status == 200){ 
     document.getElementById('rackno').innerHTML = xhr.responseText; 
    }else{ 
     throw new Error('Server has encountered an error\n'+'Error code = '+xhr.status); 
    } 
} 

function selectrackunit(){  
    var formName = 'switch'; 
    var rackname = document[formName]['rack_name'].value; 
    var location = document[formName]['location'].value; 
    var rackno = document[formName]['rackno'].value; 
    var xmlhttp = null; 
    if(typeof XMLHttpRequest != 'udefined'){ 
     xmlhttp = new XMLHttpRequest(); 
    }else if(typeof ActiveXObject != 'undefined'){ 
     xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
    }else{ 
    throw new Error('You browser doesn\'t support ajax'); 
    } 
    xmlhttp.open('GET', 'select_rack_unit.php?location='+location+'&rack='+rackname+'&rackno='+rackno, true); 
     xmlhttp.onreadystatechange = function(){ 
     if(xmlhttp.readyState == 4) 
      window.insertrackunit(xmlhttp); 
    }; 
    xmlhttp.send(null); 
} 

function selectpduunit(){  
    var formName = 'switch'; 
    var rackname = document[formName]['rack_name'].value; 
    var location = document[formName]['location'].value; 
    var rackno = document[formName]['rackno'].value; 
    var xmlhttp = null; 
    if(typeof XMLHttpRequest != 'udefined'){ 
     xmlhttp = new XMLHttpRequest(); 
    }else if(typeof ActiveXObject != 'undefined'){ 
     xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
    }else{ 
     throw new Error('You browser doesn\'t support ajax'); 
    } 
    xmlhttp.open('GET', 'select_pdu_unit.php?location='+location+'&rack='+rackname+'&rackno='+rackno, true); 
     xmlhttp.onreadystatechange = function(){ 
     if(xmlhttp.readyState == 4) 
      window.insertpduunit(xmlhttp); 
    }; 
    xmlhttp.send(null); 
} 

function insertrackunit(xhr){ 
    if(xhr.status == 200){ 
     document.getElementById('rackunits').innerHTML = xhr.responseText; 
    }else{ 
     throw new Error('Server has encountered an error\n'+'Error code = '+xhr.status); 
    } 
} 

function insertpduunit(xhr){ 
    if(xhr.status == 200){ 
     document.getElementById('rackunits').innerHTML = xhr.responseText; 
    }else{ 
     throw new Error('Server has encountered an error\n'+'Error code = '+xhr.status); 
    } 
} 

をそしてここselectpduunit機能のために働いているように見えないdoesnのPHPファイル内のコードです:

<?php 
require 'includes/opendb.php'; 
if(isset($_GET['location']) && ($_GET['rack']) && ($_GET['rackno'])) 
{  
    $b = $_GET['location']; 
    $e = $_GET['rack']; 
    $d = $_GET['rackno']; 
    $first_unit='1'; 
    $explode=explode('-',$e); 
    $last_unit=$explode[1]; 
    var_dump($b);  
    var_dump($e);  
    var_dump($d); 
    var_dump($explode); 

     while($first_unit<='4'){ //While Loop creates port columns in new table for each port on PDU. 
      if('10' > $count){ 
       $column [] = '`PDU-0'.$first_unit.'`,'; //Storing port number as string in array."`port-0".$count."` varchar (255) NOT NULL"; 
      }else{ 
       $column [] = '`'.'PDU-'.$first_unit.'`'.','; 
      } 
      ++$first_unit; 

     } 
     $empty = ''; 
     foreach($column as $c){ 
      $empty = $empty.$c; 
     } 
     //$empty = rtrim($empty2, ","); 

    $select="SELECT $empty FROM `$e` WHERE location = '$b' AND `Rack-Number` = '$d'";  
    var_dump($select); // Outputs the above generated MySQL string 
    if(!mysqli_query($conn,$select)){ 
     die("Error".mysqli_error($conn)); 
    }else{ 
     $r = mysqli_query($conn,$select); 
    } 
    $unit_1 = '1'; 
    while ('4' >= $unit_1){ 
     if($unit_1 <'10'){ 
      $column1[] ='PDU-'.'0'.$unit_1; 
     } else{ 
      $column1[] ='PDU-'.$unit_1; 
     } 
     ++$unit_1; 
    } 
    //var_dump($column1); // Outputs the above generated strings. The string should be column names. 

    $rackunit =''; 
    while($row = mysqli_fetch_assoc($r)) { 
     if($e == 'no_rack' || $e == 'vm_rack'){ 
      //$rackunit =$rackunit.'<option value="'.$row["Unit"].','."Unit".'">'.'('."Unit".')'.' '.$row["Unit"].'</option>';  
      echo "fail"; 
     }else{ 
      foreach($column1 as $c1){ 
      $rackunit =$rackunit.'<option value="'.$row["$c1"].','.$c1.'">'.'('.$c1.')'.' '.$row["$c1"].'</option>';    
      } 
     } 
    } 
    var_dump($rackunit); 
    if($rackunit == '') 
     echo 'An Error has occurred. Dropdown Menu is not being populated.'; 
    else 
     ?>  
      <select name="rackunit" id="rackunit"> 
       <option disabled="disabled" selected="selected">Rack Unit Number</option> 
       <?php echo $rackunit ?> 
       <option value="1">Test</option> 
      </select> 
     <?php 

} 

?> 

問題は何ですか?

答えて

0

コードに誤字があります。メソッドselectpduunitならびにselectrackunit

if(typeof XMLHttpRequest != 'udefined'){ 

それはあなたが新しいXMLHttpRequestオブジェクトを作成することができないという点のため、代わりに

if(typeof XMLHttpRequest != 'undefined'){ 

は可能かもしれなければなりません。試してみる。

+0

ありがとう、変更しましたが、同じ問題に直面しています – beginner

関連する問題