此处输出未显示到' #viewbook'来自数据库

时间:2017-08-03 07:34:30

标签: php ajax

<select id="viewbook" ></select>   
            <script type="text/javascript">
function getbook(val){
if( val!==''){ alert(val);}else{alert("no value");}
$.ajax({
  type:"POST",
  url:"pagination.php",
  data:{category:val},
  dataType: 'html',
  success: function(data){
    $("#viewbook").html(data);
  }
});

}  

然后这里变量&#39;类别&#39;获取并传递给&#39; pagination.php&#39;但是,除了&#34之外的数据库没有显示输出;从这里选择&#34;

<?php    
include"connection.inc";

if (!empty($_POST["category"])) {
  $pdt = $_POST['category'];
  $sql = "SELECT * FROM library_details WHERE bookcategory='$pdt'";
  $res = mysql_query($sql);
  $output = '<option value="">Select from  here</option>';
  while ($row = mysql_fetch_array($res)) {
    $output .= '<option value="' . $row["bookname"] . '">"' . $row["bookname"] . '"</option>';
  }
  echo $output;
}?>

2 个答案:

答案 0 :(得分:0)

尝试使用此代码:

<强> HTML

<select id="viewbook"></select>

Ajax电话:

<script type="text/javascript"> 
function getbook(val){
    $.ajax({
      type : 'POST',
      url : 'pagination.php',
      dataType : 'html',
      data: {
          category : val
      },
      success : function(data){
          $("#viewbook").html(data);
      },
      error : function(XMLHttpRequest, textStatus, errorThrown) {
          ("#viewbook").html("Error");
      }
    });
}
</script>

<强> PHP

<?php

 if(!empty($_POST["category"])) {
  $pdt = $_POST['category'];
  $sql = "select * from library_details where bookcategory = '$pdt' ";
  $output='';
  $res = mysql_query($sql);
  $output = '<option value="">Select from  here</option>';
  while ($row = mysql_fetch_array($res))
    $output .='<option value="'.$row["bookname"].'">'.$row["bookname"].'</option>';
  }
  //echo $row['bookname'];
  echo $output;
}

?>

答案 1 :(得分:0)

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //@formatter:off activity = getActivity(); context = getActivity().getBaseContext(); //@formatter:on setRetainInstance(true); } 更改为dataType: 'text',

dataType: 'html',

<强> pagination.php

1)连接输出

2)删除额外的<script type="text/javascript"> function getbook(val){ $.ajax({ type:"POST", url:"pagination.php", data:{category:val}, dataType: 'html', success: function(data){ $("#viewbook").html(data); } }); } </script>

3)$row = mysql_fetch_array($r);更改为while ($row = mysql_fetch_array($r)) {

4)while ($row = mysql_fetch_array($res)) {更改为$output='<optionvalue="'

5)删除$output .='<option value="'

echo $row['bookname'];