简体   繁体   中英

json data not shown in jquery

Html Code is :

<select  name="ser" id="ser" class="form-control" onchange="getPrice(this.value);">
<option value="">--Select--</option>
<option value="Value11">Value1</option>
<option value="Value2">Value2</option>
</select>
<input type="text" name="sale" id="saleprice"  class="form-control" />
<input type="text" name="sale" id="saletax"  class="form-control" />
<input type="text" name="sale" id="avalqty"  class="form-control" />

on my Js Page :

function getPrice(val)
{
   $.ajax({
     type: 'post',
     url: 'get_sales_price.php',
     data: {
       get_option:val
     },
     success: function (response) {
        var valuesar = response.split("|");
        $('#saleprice').val(valuesar[0]);
        $('#saletax').val(valuesar[1]);
        $('#avalqty').val(valuesar[2]);
     }
   });
}

This is my PHP Page Data:

$data = array();    
$values=$variable1['value1'].'|'.$variable2['value2'].'|'.$variable3;
array_push($data, $values);
echo json_encode($data);

The value on #saleprice is : ["61.25 and the value on #avalqty is : 155"] and the value on #saletax is : 1. #saletax value is correct.. How to get #saleprice : ["61.25 to 61.25 and #avalqty : 155"] to 155

I think what you can do is to return a key-value object from the server and use that in the success handler. In your case you are returning a array with a single string value

$data = array();    
$data['price'] = $variable1['value1'];
$data['tax'] = $variable2['value2'];
$data['qty'] = $variable3;
echo json_encode($data);

then

function getPrice(val) {
  $.ajax({
    type: 'post',
    url: 'get_sales_price.php',
    data: {
      get_option: val
    },
    dataType: 'json',
    success: function(response) {
        console.log(response)
      $('#saleprice').val(response.price);
      $('#saletax').val(response.tax);
      $('#avalqty').val(response.qty);
    }
  });
}

try JSON.parse(response) in your success function. You are sending your string in an array, so you need to parse it first, access the string using array index and then split this string. Otherwise, you can simply echo the string directly in your php code, rather than pushing it in an array and then using json_encode.

So your php code should be

$values=$variable1.'|'.$variable2.'|'.$variable3;
echo $data;

This way you will receive a string in your ajax response, which you can split directly.

Change

var valuesar = response.split("|"); to

 var valuesar = response[0].split("|");

hey you can use your code in following way

<?php
$data = array();    
$values='6.15'.'|'.'52.22'.'|'.'55.25';
array_push($data, $values);

$rr=json_encode($data);


$ltrim = ltrim($rr, '["');
$rtrim = rtrim($ltrim, '"]');


// echo $rtrim;



?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript">

jQuery(document).ready(function() {


var response='<?php echo  $rtrim ?>';
console.log(response);

var valuesar  = response.split("|");


$('#saleprice').val(valuesar[0]);
$('#saletax').val(valuesar[1]);
$('#avalqty').val(valuesar[2]);

 console.log(valuesar[0]);

 });

 </script>
 <!DOCTYPE html>
 <html>
 <body>

<form>
<input type="text" name="saleprice" id="saleprice" >
<input type="text" name="saletax" id="saletax" >
<input type="text" name="avalqty" id="avalqty" >
</form> 

</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM