我有一个PHP函数,我想返回下面的代码:
$myarray = array("one","two","three");
echo "<table><tr>";
foreach($myarray as $array_item)
{
echo "<td>$array_item</td>";
}
echo "</tr></table>";
这样我就可以使用json数据类型将PHP脚本传递给Ajax。
在PHP中是否有任何方法可以像变量那样返回这些代码行,以便我可以使用jQuery .html(var)
将此“变量”附加到HTML?还是其他建议?
答案 0 :(得分:1)
使用此代码
$myarray = array("one","two","three");
$str = "<table><tr>";
foreach($myarray as $array_item)
{
$str.=echo "<td>$array_item</td>";
}
$str.= "</tr></table>";
echo json_encode($str);
答案 1 :(得分:0)
json_encode你的阵列。你的jquery可以在ajax响应中获取它并放在一个变量中。在你的jquery中你可以在html表中格式化它
答案 2 :(得分:0)
$myarray = array("one","two","three");
$str = "<table><tr>";
foreach($myarray as $array_item)
{
$str .= "<td>$array_item</td>";
}
$str .= "</tr></table>";
请将此变量回显到javascript
的脚本标记中<script type="text/javascript">
var str1 = <?php echo $str;?>
</script>
将此str1变量传递给您的ajax函数....
希望这有助于你......
答案 3 :(得分:0)
使用json_encode() ..它返回值
的JSON表示 echo json_encode();
或强>
您可以将dataType用作HTML ..(您的php脚本无需更改)
dataType:'html';
success:function(html){
jquery.html(html) //apend the returned html to your div
}