2017-05-20 17 views
0

私はphpに新しいです。私は2つの選択タグを持っています。過去3日間の問題。私はjavascript phpのコードを書いており、2番目のページの名前はapi.phpです。データベースからの最初の変更時に2番目のドロップダウンで値を入力してください

<html> 
<head> 
<script src="js/jquery.min.js" ></script> 
</head> 
<body> 
    <form method="post" class="form-horizontal" > 
     <label class="col-lg-3 control-label">Category</label> 
     <select id="category" class="form-control" name="Category"> 
     <option>Select Category</option> 
     <?php 
      $con=mysqli_connect("localhost","root","","keep_shopping"); 
      $select="select * from tbl_category"; 
      $result=mysqli_query($con,$select); 
      while($row=$result->fetch_assoc()){ 
       $CategoryName=$row['CategoryName']; 
       $CategoryId=$row['CategoryId']; 
      ?> 
      <option value="<?php echo $CategoryId;?>"><?php echo $CategoryName;?></option> 
<?php 
} 
?>           
</select> 
      <br /> 
      <br /> 
      <br /> 
      <label class="col-lg-3 control-label">Brand</label> 
      <select id="brand" class="form-control" name="Brand"> 
       <option>Select a Brand</option> 
      <script> 
       $(document).ready(function(){ 
        alert('Change Happened'); 
        $('#category').change(function(){ 
        var CategoryId=$(this).val(); 
        $.ajax({ 
         'type':'get', 
         'url':'api.php', 
          'data':{'CategoryId':CategoryId,'btn':1}, 
          'success':function(response){ 
         document.getElementById("brand").innerHTML=response; 
} 
}); 
}); 
}); 
    </script> 
    </select> 
    </form> 

</body> 
</html> 

second page name is api.php 

<?php 
if(isset($_GET['btn'])){ 
$CategoryId=$_POST['CategoryId']; 
$con=mysqli_connect("localhost","root","","keep_shopping"); 
$select=mysqli_query($con,"select * from tbl_brand where 
CategoryID='$CategoryId'"); 
$states='<option>-SELECT CITY-</option>'; 
while($row=$select->fetch_assoc()){ 
$states.='<option 
value="'.$row["BrandID"].'">'.$row["BrandName"].'</option>';  
} 
echo $states; 
} 
?> 

答えて

0

AJAX機能は、メソッドのGETを使用しているが、api.phpスクリプトで、あなたがところで$_GET['CategoryId']

<?php 
    if(isset($_GET['btn'], $_GET['CategoryId'])){ 
     /* The ajax method is GET */ 
     $CategoryId=$_GET['CategoryId']; 

     $con=mysqli_connect("localhost","root","","keep_shopping"); 
     $select=mysqli_query($con,"select * from tbl_brand where CategoryID='$CategoryId'"); 
     $states='<option>-SELECT CITY-</option>'; 

     while($row=$select->fetch_assoc()){ 
      $states.='<option value="'.$row["BrandID"].'">'.$row["BrandName"].'</option>';  
     } 
     echo $states; 
    } 
?> 

にそれを変更するべきであるので、おそらく$_POST['CategoryId']を使用しようとしている - api.phpコードが脆弱ですSQL injectionにする必要があります。prepared statements

<script> 
    $(document).ready(function(){ 
     /* The alert should be within the change callback function */ 
     $('#category').change(function(){ 
      var CategoryId=$(this).val(); 
      alert('Change Happened: '+CategoryId); 
      $.ajax({ 
       'type':'get', 
       'url':'api.php', 
       'data':{'CategoryId':CategoryId,'btn':1}, 
       'success':function(response){ 
        document.getElementById("brand").innerHTML=response; 
       } 
      }); 
     }); 
    }); 
</script> 

onchangeイベントが実際に発生しているかどうかをテストするには、コンソールを使用してください(ほとんどのブラウザでは現在コンソールが使用されています)。

上記のjavascriptを使用してテストドキュメントをセットアップしただけで、アラートがトリガされてデータが送信されましたが、別のターゲットスクリプトを使用していました。

+0

まだjavascriptとして機能していないため、アラートも表示されません... – Ashish

+0

変更イベントが有効かどうかを確認する方法 – Ashish

関連する問題