我试图在Ajax中显示值我在employee.php页面中获取数组值,但是当我将值传递给getdata.js时。它没有显示结果。
检查以下结果:>
echo json_encode($data); [{"0":"2","tax_id":"2","1":"GST","tax_type":"GST","2":"1","tax_comp":"1","3":"10","tax_Percent":"10"},{"0":"3","tax_id":"3","1":"CGST","tax_type":"CGST","2":"1","tax_comp":"1","3":"9","tax_Percent":"9"},{"0":"8","tax_id":"8","1":"new child","tax_type":"new child","2":"1","tax_comp":"1","3":"15","tax_Percent":"15"}]
-------------- getEmployee.php ------------------------------ ------------------
if($_REQUEST['tax_id']) {
$sql = "SELECT tax_id, tax_type, tax_comp, tax_Percent FROM ca_taxmaster WHERE tax_comp='".$_REQUEST['tax_id']."'";
$resultset = mysql_query($sql) or die(mysql_error());
$data = array();
while( $rows = mysql_fetch_array($resultset) ) {
$data[] = $rows;
// $data[] = $rows['tax_id'] . " " . $rows['tax_type'] . " " . $rows['tax_comp'] . " " . $rows['tax_Percent'];
}
echo json_encode($data);
} else {
echo 0;
}?>
------------------------------------- getData.js ------- -------------------------------------------------- --------------- 路径: - resources / js / getData.js">
$(document).ready(function(){
// code to get all records from table via select box
$("#employee").change(function() {
var tax_id = $(this).find(":selected").val();
var dataString = 'empid='+ tax_id;
$.ajax({
url: 'http://localhost/capms_v2_feb/ajax/getEmployee.php',
dataType: "json",
data: dataString,
cache: false,
success: function(employeeData) {
//alert(data);
if(employeeData) {
$("#heading").show();
$("#no_records").hide();
$("#tax_type").text(employeeData.tax_type);
$("#tax_comp").text(employeeData.tax_comp);
$("#tax_Percent").text(employeeData.tax_Percent);
$("#records").show();
} else {
$("#heading").hide();
$("#records").hide();
$("#no_records").show();
}
}
});
})
});
-------------------------------------查看--------- ---------------------------
<select id="employee">
<option value="" selected="selected">Please select</option>
<?php
$sql = "SELECT tax_id, tax_type, tax_comp, tax_Percent FROM ca_taxmaster where tax_comp = '0'";
$resultset = mysql_query($sql) or die( mysqli_error());
while( $rows = mysql_fetch_array($resultset)) {
?>
<option value="<?php echo $rows["tax_id"]; ?>"><?php echo $rows["tax_type"]; ?></option>
<?php } ?>
</select>
<div id="display">
<div class="row" id="heading" style="display:none;"><h3><div class="col-sm-4"><strong>Employee Name</strong></div><div class="col-sm-4"><strong>Age</strong></div><div class="col-sm-4"><strong>Salary</strong></div></h3></div><br>
<div class="row" id="records">
<div class="col-sm-4" id="tax_type"></div>
<div class="col-sm-4" id="tax_comp"></div>
<div class="col-sm-4" id="tax_Percent"></div>
</div>
<div class="row" id="no_records"><div class="col-sm-4">Plese select employee name to view details</div></div>
如果有人知道提前帮助我
请点击以下链接:
答案 0 :(得分:1)
你可以这样做:
var employeeData = [{"0":"2","tax_id":"2","1":"GST","tax_type":"GST","2":"1","tax_comp":"1","3":"10","tax_Percent":"10"},{"0":"3","tax_id":"3","1":"CGST","tax_type":"CGST","2":"1","tax_comp":"1","3":"9","tax_Percent":"9"},{"0":"8","tax_id":"8","1":"new child","tax_type":"new child","2":"1","tax_comp":"1","3":"15","tax_Percent":"15"}];
employeeData.forEach(function(item) {
var data = '<tr>';
data+= '<td>'+item.tax_type+'</td>';
data+= '<td>'+item.tax_comp+'</td>';
data+= '<td>'+item.tax_Percent+'</td>';
data+='</tr>';
$('.appendData').append(data);
});
&#13;
<table>
<thead>
<th>Type</th>
<th>Camp</th>
<th>Percentage</th>
</thead>
<tbody class="appendData">
</tbody>
</table>
&#13;
答案 1 :(得分:0)
你应该尝试添加标题(“Content-type:application / json”);在你的php文件上回显json_encode之前。
请尝试更改以下内容:
if($_REQUEST['tax_id']) to $_REQUEST['empid']
$sql = "SELECT tax_id, tax_type, tax_comp, tax_Percent FROM ca_taxmaster WHERE tax_comp='".$_REQUEST['tax_id']."'";
to
$sql = "SELECT tax_id, tax_type, tax_comp, tax_Percent FROM ca_taxmaster WHERE tax_comp='".$_REQUEST['empid']."'";
OR try this
Change var dataString = 'empid='+ tax_id; to var dataString = 'tax_id ='+ tax_id;