2017-07-03 83 views
-1

我希望获取通过jQuery的阿贾克斯PHP价值。但是当我在这个代码下运行的时候,这个值是正确的,但是当所有的值都被提取并且一起打印所有的字段时,所有的值都被连接在一起,但是我想在单独的字段中打印单独的值。我的代码如下,请解决这个问题jQuery的阿贾克斯数值读取

的index.php

<!DOCTYPE html> 
<html> 
<head> 
<title>Untitled Document</title> 
</head> 
<body> 
<select name="Pro" onChange="my_validate_func()" id="pro_serv"> 
<option value=""></option> 
<option value="" ><font size="-1">Add New Product</font></option> 
<option value="Development"><font size="-1">Development</font></option> 
<option value="Maintance"><font size="-1">Maintance</font></option> 
</select> 
<br /> 
<input type="text" name="desc" id="desc" value="" /> 
<br /> 
<input type="text" name="unitprice" id="unitprice" value="" /> 
<br /><input type="text" name="discount" /> 
<script type="text/javascript" src="bootstrap/jquery1.8.3jquery.min.js"> 
</script> 
<script> 
$(document).ready(function() {}); 
function my_validate_func() { 
var pro_serv = $('#pro_serv').val(); 

    if ($('#pro_serv').val() != "") { 
     $.ajax({ 
      type: "POST", 
      url: 'check.php', 
      data: { pro_serv: pro_serv}, 
      success: function(response) { 
       $('#desc').val(response); 
       $('#unitprice').val(response); 
       $('#tax').val(response); 

      } 

     }); 
    } 
    } 
</script> 
</body> 
</html> 

check.php

<?php 
include('database/db.php'); 
$type=$_POST['pro_serv']; 
$sql="Select * from product_service_details where type='$type'"; 
$result=mysql_query($sql); 
if($dtset=mysql_fetch_array($result)) 
{ 
    $desc=$dtset['desc']; 
    $unitprice=$dtset['unitprice']; 
    $tax=$dtset['tax']; 
     echo $desc; 
     echo $unitprice; 
     echo $tax; 
} 


?> 
+1

'mysql_ *'功能已被取消,在PHP7删除 - 出于安全和面向未来的考虑,更改为mysqli – ThisGuyHasTwoThumbs

+0

这是因为'response'包含所有三个变量的数据。你需要用'split'那些 – urfusion

+0

如何分割该数据 – Susant

回答

3

你需要PHP和jQuery的使用json_encode你需要解析像下面的回应:

PHP:

<?php 
include('database/db.php'); 
$type=$_POST['pro_serv']; 
$sql="Select * from product_service_details where type='$type'"; 
$result=mysql_query($sql); 
if($dtset=mysql_fetch_array($result)) 
{ 
    echo json_encode($dtset); 
} 

JQuery的:

 $.ajax({ 
      type: "POST", 
      url: 'check.php', 
      data: { pro_serv: pro_serv}, 
      success: function(response) { 
      var res = $.parseJSON(response); 
       $('#desc').val(res.desc); 
       $('#unitprice').val(res.unitprice); 
       $('#tax').val(res.tax); 

      } 

     }); 
+0

检查这个@Susant – lalithkumar

+0

感谢@latithkumar – Susant

+1

而且也不用mysql_ * function..move到mysqli_ *功能.. – lalithkumar

0

您可以使用单独的值的分离。接着用JSsplit功能。

echo $desc ."-" . $unitprice ."-" .$tax; 

JS中

var arr = response.split('-'); 
$('#desc').val(arr[0]); 
$('#unitprice').val(arr[1]); 
$('#tax').val(arr[2]); 
0

在check.php

*echo $tax;*应该echo json_encode('tax'=>$tax);

和index.php文件

$('#tax').val(response);可以$('#tax').val(response.tax);