使用 JSON 从 PHP 返回数组并以 HTML 显示内容的适当方法
appropriate way of using json to return array from php and displaying contents in html
下面是 ajax 代码。
问题在评论中
$.ajax({
type: "POST",
url: "dbtryout2_2.php",
data:datastr,
dataType: "json",
success: function(arrayphp)
{
//ARRAYPHP is a json array and iam displaying the
//contents of the array one by one as link
$.each(arrayphp, function(i, element) {
var link = $('<a href="#" class="album"><font color="red">' + element + '</font></a>');
var image='<input type="image" src="img4.jpg" height="30px" width="30px">';
$(".searchby .searchlist").append(image).append(link);
});
}
});
上面的代码不是 working.am 我没有正确使用json数组下面是PHP代码/PHP代码/
if(!strcmp($_REQUEST['id'],'a2'))
{
$lang=$_REQUEST['lang'];$type=$_REQUEST['type'];
$query="select distinct(cat_name) from song where lang='$lang' and category='$type'";
$result=mysql_query($query,$con) or die("Query failed".mysql_error());
$arrayphp=array();
while($row=mysql_fetch_array($result))
{
$element=$row['cat_name'];
array_push($arrayphp,$element);
}
//here is the json array that is being sent to the above html code
echo json_encode($arrayphp);
}
您需要将从 PHP 传递的字符串转换为正确的 JSON 对象/数组,然后才能用作 JavaScript 对象/数组。
success: function(arrayphp) {
arrayphp = jQuery.parseJSON(arrayphp); // will make proper json from a json string
//ARRAYPHP is a json array and iam displaying the
//contents of the array one by one as link
$.each(arrayphp, function(i, element) {
....
相关文章:
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- 用HTML在不同屏幕上显示和隐藏内容的方法
- 显示指令时调用指令方法
- Push方法显示错误
- 使用较少代码隐藏和显示选择菜单内容的更好方法是什么?javascript
- 设置显示后Flash对象方法不可用:无
- 重置transform:rotate(),方法是移除并追加在追加和重绘图表后未显示数据的画布
- 什么'这是用javascript隐藏和显示表中所有行的最快方法
- ng显示“;调用方法“;不起作用
- 如何访问新的切换突出显示方法
- 在列表框中显示方法的结果
- Javascript 显示方法中的所有(元素、标签)元素由标签名称
- CodeIgniter:根据所选控制器名称显示方法名称
- $compile'd指令中显示方法未执行
- 为什么我不能显示'元素的显示方法
- PHP在页面上显示方法的进度
- 更改隐藏/显示方法
- 在node.js web服务器上生成图像后,最简单的显示方法是什么(使用express)
- 如何使用jQuery在索引方法上加载Laravel中显示方法的内容
- 如何附加显示方法