从php数组中获取数据

编程入门 行业动态 更新时间:2024-10-27 14:27:13
本文介绍了从php数组中获取数据 - AJAX - jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个页面如下;

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

和一个php文件 ajax.php 到处理ajax请求为;

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

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

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

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

提前致谢...

推荐答案

你无法从js 尝试访问数组(php数组)

you cannot access array (php array) from js try

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

和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]); }, }); }); });

更多推荐

从php数组中获取数据

本文发布于:2023-10-12 02:56:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483549.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   数据   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!