0

I am trying to show the values in Ajax I was getting array values in employee.php page but when I pass the value to getdata.js .It was not showing the result.

Check below Result for this:: >

 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 ------------------------------------------------------------------------ Path:- 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();
        }
      }
     });
    })
   });

------------------------------------- View ------------------------------------

  <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>

If anyone knows help me thanks in advance

Please click below link:

database screenshot

enter image description here

I need ouput like this screenshot

enter image description here

2
  • You should try adding header("Content-type:application/json"); on your php file. Commented Feb 16, 2018 at 5:41
  • You're open to a sql injection attack btw: localhost/capms_v2_feb/ajax/… Commented Feb 16, 2018 at 5:48

2 Answers 2

1

You can do as in this way:

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>

Sign up to request clarification or add additional context in comments.

Comments

0

You should try adding header("Content-type:application/json"); before you echo json_encode on your php file.

And please try changing the following lines:

 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;

4 Comments

@Jennifer Could you run "console.log(employeeData);" on your success ajax and see what it shows?
@Jennifer I found out that your condition is checking for $_REQUEST['tax_id'] which is incorrect. Change it to $_REQUEST['empid']
thank you cjatstackoverflow it's working now $_REQUEST['empid'] is corrected but i asked for array of value in this way var employee = [employeeData]; 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); }); now it's working fine thanks for you valuable time
@Jennifer Please set this as an answer if that solves your problem. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.