【问题标题】:how to get array values in ajax如何在ajax中获取数组值
【发布时间】:2018-02-16 04:43:57
【问题描述】:

我试图在 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>

如果有人知道请帮助我提前谢谢

请点击以下链接:

database screenshot

I need ouput like this screenshot

【问题讨论】:

  • 你应该尝试添加 header("Content-type:application/json");在你的 php 文件中。
  • 您对 sql 注入攻击持开放态度 btw:localhost/capms_v2_feb/ajax/…

标签: javascript php mysql ajax codeigniter


【解决方案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);
});
<table>
  <thead>
    <th>Type</th>
    <th>Camp</th>
    <th>Percentage</th>
  </thead>
  <tbody class="appendData">
    
  </tbody>
</table>

【讨论】:

    【解决方案2】:

    您应该尝试添加 header("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;
    

    【讨论】:

    • @Jennifer 你能运行“console.log(employeeData);”吗?在你的成功 ajax 上看看它显示了什么?
    • @Jennifer 我发现您的情况正在检查不正确的 $_REQUEST['tax_id']。将其更改为 $_REQUEST['empid']
    • 谢谢 cjatstackoverflow 现在可以正常工作 $_REQUEST['empid'] 已更正,但我以这种方式要求值数组 var employee = [employeeData]; employeeData.forEach(function(item) { var data = ''; data+= ''+item.tax_type+''; data+= ''+item.tax_comp+' td>'; data+= ' '+item.tax_Percent+''; data+=''; $('.appendData').append(data); });现在一切正常,感谢您宝贵的时间
    • @Jennifer 如果可以解决您的问题,请将此设置为答案。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2021-01-10
    • 2015-11-10
    • 2020-09-16
    • 2013-09-09
    • 2016-04-23
    • 2013-11-06
    相关资源
    最近更新 更多