【问题标题】:array_push doesn't push the value into array and prints only last pushed valuearray_push 不会将值推送到数组中,并且仅打印最后推送的值
【发布时间】:2014-03-04 09:02:12
【问题描述】:

array_push 仅返回当前已在 php 中推入数组的最后一个值。请帮忙。这是我工作的链接http://dev.optimizebusinessgrowth.com/gopaperboy/members/ 它是一个 jquery ajax。我用

调用 ajax
jQuery(function(){
  jQuery('.icon-heart').click(function(){

    var user_IDs = jQuery(this).attr('id');
    var current_IDs = <?php echo $cui ?>;

    jQuery.ajax({
        url: '<?php bloginfo('url') ?>/',
        type: 'POST',
        data: 'ajaxreturn=101&id='+user_IDs+'&myid='+current_IDs,
        success: function(result){
          jQuery('.testres').html(result);
        }

    });//ajax ends here 
  });
});

这是function.php中给出响应的代码

function my_ajaxreturn_101() {
  $current_fav = array();
  if($_POST['ajaxreturn']==101) {

    $userID = $_POST['id']; 
    $loggedID = $_POST['myid'];

    array_push($current_fav,$userID);
    print_r($current_fav);
    exit;

  }

}

【问题讨论】:

  • 这是PHP吗?请为您的编程语言添加标签。
  • 因为每次调用函数时都要重新定义$current_fav,所以每次都是一个新数组,也就是“最后一个值”
  • 你在哪里打印数组?
  • 其实这里是我工作的链接。 dev.optimizebusinessgrowth.com/gopaperboy/members
  • 它的 jquery ajax。我通过这个调用ajax

标签: php array-push


【解决方案1】:

尝试使用append()而不是html(),这样你可以这样做:

jQuery('.testres').append(result);

代替:

jQuery('.testres').html(result);

【讨论】:

  • append 再次追加具有相同值的数组。我只是打印 .testres 中的值以确定数据是否被推入数组。之后,我将通过序列化将其插入数据库。谢谢
猜你喜欢
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多