从php数组- AJAX - jQuery获取数据

时间:2022-10-08 18:41:27

I have a page as below;

我有如下一页;

<head>
<script type="text/javascript" src="jquery-1.6.1.js"></script>
<script type="text/javascript">
$(document).ready( function() {
$('#prev').click(function() {
  $.ajax({
  type: 'POST',
  url: 'ajax.php',
  data: 'id=testdata',
  cache: false,
  success: function(result) {
    $('#content1').html(result[0]);
  },
  });
});
});
</script>
</head>
<body>
<table>
<tr>
<td id="prev">prev</td>
<td id="content1">X</td>
<td id="next">next</td>
</tr>
</table>
</body>

and a php file ajax.php to handle ajax requests as;

还有一个php文件ajax。php来处理ajax请求;

<?php
$array = array(1,2,3,4,5,6);
echo $array;
?>

But when I click, I am getting A instead of array[0]. How can I fix this??

但当我点击时,我得到的不是数组[0]而是A。我怎么解决这个问题?

Thanks in advance...

提前谢谢…

5 个解决方案

#1


52  

you cannot access array (php array) from js try

无法从js try中访问数组(php数组)

<?php
$array = array(1,2,3,4,5,6);
echo json_encode($array);
?>

and js

和js

$(document).ready( function() {
    $('#prev').click(function() {
        $.ajax({
            type: 'POST',
            url: 'ajax.php',
            data: 'id=testdata',
            dataType: 'json',
            cache: false,
            success: function(result) {
                $('#content1').html(result[0]);
            },
        });
    });
});

#2


18  

quite possibly the simplest method ...

很可能是最简单的方法……

<?php
$change = array('key1' => $var1, 'key2' => $var2, 'key3' => $var3);
echo json_encode(change);
?>

Then the jquery script ...

然后jquery脚本…

<script>
$.get("location.php", function(data){
var duce = jQuery.parseJSON(data);
var art1 = duce.key1;
var art2 = duce.key2;
var art3 = duce.key3;
});
</script>

#3


8  

When you echo $array;, the result is Array, result[0] then represents the first character in Array which is A.

当您回显$array时,结果是array,结果[0]表示数组中的第一个字符,即A。

One way to handle this problem would be like this:

解决这个问题的一种方法是:

ajax.php

ajax.php

<?php
$array = array(1,2,3,4,5,6);
foreach($array as $a)
    echo $a.",";
?>

jquery code

jquery代码

$(function(){ /* short for $(document).ready(function(){ */

    $('#prev').click(function(){

        $.ajax({type:    'POST',
                 url:     'ajax.php',
                 data:    'id=testdata',
                 cache:   false,
                 success: function(data){
                     var tmp = data.split(",");
                     $('#content1').html(tmp[0]);
                 }
                });
    });

});

#4


3  

you cannot access array (php array) from js try

无法从js try中访问数组(php数组)

<?php
$array = array(1,2,3,4,5,6);
echo implode('~',$array);
?>

and js

和js

$(document).ready( function() {
$('#prev').click(function() {
  $.ajax({
  type: 'POST',
  url: 'ajax.php',
  data: 'id=testdata',
  cache: false,
  success: function(data) {
    result=data.split('~');
    $('#content1').html(result[0]);
  },
  });
});
});

#5


0  

When you do echo $array;, PHP will simply echo 'Array' since it can't convert an array to a string. So The 'A' that you are actually getting is the first letter of Array, which is correct.

当您回显$array时,PHP只回显' array ',因为它不能将数组转换为字符串。所以你得到的A是数组的第一个字母,是正确的。

You might actually need

你可能会需要

echo json_encode($array);

This should get you what you want.

这应该能得到你想要的。

EDIT : And obviously, you'd need to change your JS to work with JSON instead of just text (as pointed out by @genesis)

编辑:显然,您需要更改您的JS以使用JSON而不仅仅是文本(如@genesis所示)

#1


52  

you cannot access array (php array) from js try

无法从js try中访问数组(php数组)

<?php
$array = array(1,2,3,4,5,6);
echo json_encode($array);
?>

and js

和js

$(document).ready( function() {
    $('#prev').click(function() {
        $.ajax({
            type: 'POST',
            url: 'ajax.php',
            data: 'id=testdata',
            dataType: 'json',
            cache: false,
            success: function(result) {
                $('#content1').html(result[0]);
            },
        });
    });
});

#2


18  

quite possibly the simplest method ...

很可能是最简单的方法……

<?php
$change = array('key1' => $var1, 'key2' => $var2, 'key3' => $var3);
echo json_encode(change);
?>

Then the jquery script ...

然后jquery脚本…

<script>
$.get("location.php", function(data){
var duce = jQuery.parseJSON(data);
var art1 = duce.key1;
var art2 = duce.key2;
var art3 = duce.key3;
});
</script>

#3


8  

When you echo $array;, the result is Array, result[0] then represents the first character in Array which is A.

当您回显$array时,结果是array,结果[0]表示数组中的第一个字符,即A。

One way to handle this problem would be like this:

解决这个问题的一种方法是:

ajax.php

ajax.php

<?php
$array = array(1,2,3,4,5,6);
foreach($array as $a)
    echo $a.",";
?>

jquery code

jquery代码

$(function(){ /* short for $(document).ready(function(){ */

    $('#prev').click(function(){

        $.ajax({type:    'POST',
                 url:     'ajax.php',
                 data:    'id=testdata',
                 cache:   false,
                 success: function(data){
                     var tmp = data.split(",");
                     $('#content1').html(tmp[0]);
                 }
                });
    });

});

#4


3  

you cannot access array (php array) from js try

无法从js try中访问数组(php数组)

<?php
$array = array(1,2,3,4,5,6);
echo implode('~',$array);
?>

and js

和js

$(document).ready( function() {
$('#prev').click(function() {
  $.ajax({
  type: 'POST',
  url: 'ajax.php',
  data: 'id=testdata',
  cache: false,
  success: function(data) {
    result=data.split('~');
    $('#content1').html(result[0]);
  },
  });
});
});

#5


0  

When you do echo $array;, PHP will simply echo 'Array' since it can't convert an array to a string. So The 'A' that you are actually getting is the first letter of Array, which is correct.

当您回显$array时,PHP只回显' array ',因为它不能将数组转换为字符串。所以你得到的A是数组的第一个字母,是正确的。

You might actually need

你可能会需要

echo json_encode($array);

This should get you what you want.

这应该能得到你想要的。

EDIT : And obviously, you'd need to change your JS to work with JSON instead of just text (as pointed out by @genesis)

编辑:显然,您需要更改您的JS以使用JSON而不仅仅是文本(如@genesis所示)