[英]Is it possible to print javascript array in the same php page
此页面是mypage.php
。 我需要在同一页面上发布数组。 我有一个输入字段,当我单击按钮时,该值应保存到数组中。然后我需要在不刷新页面的情况下显示数组中的所有值。
<input name="name">
<table>
<td>Name</td>
<?php
if(isset($POST['res'])){
$myarr = json_encode($_POST['res']);
foreach($myarr as $a){ ?>
<tr><td><?php echo $a['product'] ?> </td></tr>
<?php } } ?>
</table>
<i onclick="sumpup()" class="glypcon glyphicon-arrow-right" ></i>
<script>
var myarr=[];
function sumup() {
var a = document.getElementById('name').value;
myarr.push({'product': a});
$.ajax({
type: 'POST',
url : 'mypage.php',
data: {res: JSON.stringify(myarr)},
success: function (response) {
} })
}
</script>
我需要在同一页面中发布数组,并在表中显示值。 重要的是,该页面不应重新加载,当我单击按钮时,我应该显示带有数组的表。
实际上是将数组作为参数。 但是我不能打印数组。 请帮我。
$ myarr = json_encode($ _ POST ['res']);
这应该是$ myarr = json_decode($ _POST ['res'],true);
您已经将红色字符串化了,您需要将其转换为php数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.