2017-03-09 8 views
0

ここに私のコードで私は取得し、onChangeを使用してドロップダウンからオプションを選択した後にデータを表示しようとしている、PHPファイルからデータをフェッチし、同じにテキストエリアに表示するajax経由でselect .phpファイルが残念ながら私のために働いていない混乱終了した私は間違いをした、これで私を助けてください。onChange jquery ajaxの問題を使用してデータを取得する

select.php

<head> 
<script type="text/javascript"> 
$(document).ready(function() { 
$("#channel").change(function(){ 
    $.post("ajax.php", { channel: $(this).val() }) 
    .success(function(data) { 

      $(".result").html(data); 
     }); 
    }); 
}); 
</script> 
</head> 
<div class="col-sm-6 form-group"> 
    <select class="chosen-select form-control" id = 'channel' name="ProductCategoryID" value="<?php echo set_value('ProductCategoryID'); ?>" required> 
     <option>Select Item code</option> 
     <?php 
      foreach($itemlist as $row) 
      { 
       echo '<option value="1234">'.$row->ItemCode.'</option>'; 
      } 
     ?> 
    </select> 
</div> 
<div class="col-sm-12 form-group result"></div> 

ajax.php

<?php 
define('HOST','localhost'); 
define('USER','***'); 
define('PASS','***'); 
define('DB','***'); 

$response = array(); 

$conn = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect'); 

//get value from page 

$channel = $_POST['channel']; 

$query = "SELECT * FROM gst_itemmaster where ItemCode = '$channel' "; 
$result = mysqli_query($conn,$query); 
$msg = ''; 
while($row = mysqli_fetch_array($result)) { 
    $msg = $msg. '<textarea type="text" class="form-control" name="Description"></textarea>'.$row['ItemDescription'].'</textarea>'; 
} 

echo $msg; 
+1

console.log(data)を追加できます。 Ajax呼び出しの成功関数で、error_reporting(E_ALL)を追加します。あなたのajax.phpでブラウザのコンソールにサーバーのエラーや通知が表示されます。 –

答えて

0
while($row = mysql_fetch_array($result)) { 

     $msg = $msg. '<textarea type="text" class="form-control" name="Description"></textarea>'.$row['ItemDescription'].'</textarea>'; 
} 

使用してみてください:

while($row = mysqli_fetch_array($result)) { 

     $msg = $msg. '<textarea type="text" class="form-control" name="Description"></textarea>'.$row['ItemDescription'].'</textarea>'; 
} 

役立つかもしれない

+0

お返事ありがとうございました。私はそのおかげで逃しましたが、私はその修正後も同じ問題に直面しています..! –

+0

SELECT * FROM gst_itemmasterここでItemCode = '$ channel' 私はあなたの質問が何であるか分かりませんが、正しい構文は上記のようにすべきだと思います。 – vishi

+0

更新されたコードを参照してくださいが、まだ動作していません –

0

最初に$ msgを起動して、mysqliモジュールを使用してください。

define('HOST','localhost'); 
define('USER','***'); 
define('PASS','***'); 
define('DB','***'); 

$response = array(); 

$conn = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect'); 

//get value from page 

$channel = $_POST['channel']; 

$query = "SELECT * FROM gst_itemmaster where ItemCode =$channel"; 
$result = mysqli_query($conn,$query); 

$msg = ''; 

while($row = mysqli_fetch_array($result)) { 

    $msg = $msg. '<textarea type="text" class="form-control" name="Description"></textarea>'.$row['ItemDescription'].'</textarea>'; 
} 

echo $msg; 

UPDATE を使用してPOSTリクエストを更新します。

$.post("ajax.php", 
    { channel: $(this).val() }, 
    function(data) { 
     $(".result").html(data); 
    } 
); 

OR

$.post("ajax.php", 
    { channel: $(this).val() }, 
    successCallback 
); 

function successCallback(data){ 
    //process data.. 
} 

、.post $( "ajax.pを交換https://api.jquery.com/jquery.post

+0

私は今、同じ問題を..私のコードを更新しました! –

+0

@vishiが彼のコメントで述べたように、$ channel変数をクエリ内の単一のqouteに囲んでみてください(例えば "SELECT * FROM gst_itemmaster where ItemCode = '$ channel'")。 – franzfloresjr

+0

更新されたコードを見てください。 –

0

を見ますHP」、{チャネル:$(この).val()}) と.post $( "ajax.php"、{ 'チャネル':$(この).val()})

0
$.post("ajax.php", { channel: $(this).val() },function(data) { 
    $(".result").html(data); 
}); 

コードから.success(function(data){})を削除してください。

関連する問題