English 中文(简体)
$.ajaax 值未正确返回
原标题:$.ajax values not returning correctly
  • 时间:2012-05-26 17:07:53
  •  标签:
  • php
  • jquery

I am trying to get returned values from database from a PHP array to a JavaScript array. So far what I ve done is returning the values as a string and I want each value as an array index.

这是我的JJQuery AJAX电话:

    $.ajax({
          type:  GET ,
          url: "Profile_Control.php",
          data:"ajaxRequest=yes&id="+document.getElementById("compid").value,
          success:function(data)
          {       
              document.getElementById("asd").innerHTML=data;
          }
        });

这是我迄今为止的 PHP 脚本, 它正在像字符串一样返回值, 我想要数组索引中的每一个值 。

   $branches=Profile_Model::getCompanyBranches($_GET[ id ]);

    while($row=mysql_fetch_array($branches))
    {
        echo $row[3];
    }

现在我只重复从数据库返回的值的第3列, 输出是这样的:

        67.030867.020467.031167.020667.0357

我想得到的结果应该是这样

   arr[0]=67.0308
   arr[1]=67.0204
   arr[2]=67.0311
   arr[3]=67.0206
   arr[4]=67.0357

我还尝试用 json_encode($row[3]); 来编码JSON的数据,但它返回了以下结果。

  "67.0308""67.0204""67.0311""67.0206""67.0357"
最佳回答

在您的 JS 数组中创建包含您最终想要的元素的数组, 然后在数组中创建 json_encode :

$rows = array();
while($row=mysql_fetch_array($branches)) {
    $rows[] = $row[3];
}
echo json_encode($rows);

然而, data 在您成功回调时将是一个阵列, 这样您无法简单地将其指定为元素的 < code> innerHTML 属性。 但是, 既然这似乎是调试输出, 只需将它替换为 < code> concolole.log( data); 。

问题回答

创建每个元素都 $row[3] 的新阵列,通过 json_encode 发送新阵列。

现在,您正在分别将每个 $row[3] 编码为json_encode。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签