如何在jquery中添加返回值

How to append the return values in jquery

本文关键字:添加 返回值 jquery      更新时间:2023-09-26

这里我从数据库中选择了匹配的记录,之后我在上一页返回,在这里我得到了所有的值,但我不知道如何在这个页面中添加这个返回值,我需要像而不是房间2,房间3…value中追加值。room_number and PG NAME HERE append in value.bg_id


我得到了这样的值

var res =jQuery.parseJSON(data);
         console.log(res);

count:2 data:Array[2] 0:Object 1:Object

booking_status:"1"

id:"2"

pg_id:"1"

租金:"4000"

room_number:"房间2"

room_sharing:"2"


booking_status:"1"

id:"3"

pg_id:"1"

租金:"4000"

room_number:"房间3"

room_sharing:"2"

index.php
<script>
function showDiv(toggle){
var sharing=$("#sharing").val();
$.ajax({
	     type: "POST",
		 url: "pg_details.php",
	     data: "sharing_id="+sharing,
		 success: function(data) {
		 var res =jQuery.parseJSON(data);
		 console.log(res);
		 if(res['return'] == 1){
			   var htmlString=''; 
			   $.each( res['data'], function( key, value ){
			  htmlString ='<div id="toggle"><div class="container" style=" margin-bottom: 30px;"><div class="row"><h4 style="margin-left:15px;">'+value.pg_id+'</h4><div class="col-sm-10"><div class="btn-group" id="btnmar"><a href="register.php?id=2"><button  type="button" class="btn btn-primary" style=" width: 71px; ">'+value.room_number+'</button></a>&nbsp;&nbsp;</div></div><div class="col-sm-2"><div class="panel-group"><div class="panel panel-primary"><div class="panel-heading"> Premium Facility</div><div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-television" aria-hidden="true" style="margin-right:15px;"></i>T.V.</div><div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-wifi" aria-hidden="true" style="margin-right:15px;"></i>Wifi</div><div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-bed" aria-hidden="true" style="margin-right:15px;"></i>Bed</div><div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-shopping-basket" aria-hidden="true" style="margin-right:15px;"></i>Washing Machine</div></div></div></div></div></div></div>';
				
			});
			$(".view_room").prepend(htmlString);
			
		}
	     }
	  });
}
</script>
pg_details.php
<?php
include_once("admin/config.php");
include("functions.php");
//$pg_name=Getpg($_POST['pg_id']);
$sharing=$_POST['sharing_id'];//Getting Sharing Value
$sql=mysql_query("SELECT * FROM rooms WHERE room_sharing='$sharing'");
$count = mysql_num_rows($sql);
if($count > 0){
	while($row=mysql_fetch_assoc($sql)){
	$data[]= $row;
	}
	$pg_type= array("return"=>1,"count" =>$count,"data" =>$data);
    echo $pg_type = json_encode($pg_type);
}else{
	$pg_type= array("return"=>0,"count" =>0,"data" =>"");
    echo $pg_type = json_encode($pg_type);
}
?>
<div class="view_room"></div>

尝试如下:

$.ajax({
  type: "POST",
  url: "pg_details.php",
  data: "sharing_id=" + sharing,
  success: function(data) {
    var res = jQuery.parseJSON(data);
    $.each(res.data, function(key, value) {
     el = $('[data-id='+value.pg_id +']');
     if (el.length) {
         el.find('.btnmar').append(' <a href="register.php?id='+value.id +'">
            <button type="button" class="btn btn-primary" style=" width: 71px; ">'+value.room_number+'</button>
          </a>&nbsp;&nbsp;');
      } else {
     var htmlString =  '<div id="toggle" data-id="'+value.pg_id +'">
 <div class="container" style=" margin-bottom: 30px;">
    <div class="row">
      <h4 style="margin-left:15px;">'+value.pg_id +'</h4>
      <div class="col-sm-10">
        <div class="btn-group btnmar">
          <a href="register.php?id='+value.id +'">
            <button type="button" class="btn btn-primary" style=" width: 71px; ">'+value.room_number+'</button>
          </a>&nbsp;&nbsp;
        </div>
      </div>
      <div class="col-sm-2">
        <div class="panel-group">
          <div class="panel panel-primary">
            <div class="panel-heading"> Premium Facility</div>
            <div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-television" aria-hidden="true" style="margin-right:15px;"></i>T.V.</div>
            <div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-wifi" aria-hidden="true" style="margin-right:15px;"></i>Wifi</div>
            <div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-bed" aria-hidden="true" style="margin-right:15px;"></i>Bed</div>
            <div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-shopping-basket" aria-hidden="true" style="margin-right:15px;"></i>Washing Machine</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>';
   $(".view_room").prepend(htmlString);}
    });

  }
});

php for pg_name:

while($row=mysql_fetch_assoc($sql)){
    $row['pg_name'] = Getpgname($row['pg_id']);
    $data[]= $row;
    }

或使用连接查询同时返回名称和数据