如何把每个PHP的foreach值到每个jquery ajax [英] how to put each php foreach value into each jquery ajax

查看:108
本文介绍了如何把每个PHP的foreach值到每个jquery ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foreach($data['data'] as $data){
    $count = $data['number'];
}

// $data['number']; will return some number like:

159809
359107
249178
... //10+ numbers

然后,如何将每个PHP foreach值放入每个 jquery ajax 中? (把每个数字通过一个jQuery的Ajax,将有10 + Ajax调用,并返回所有的数据在一个div)。谢谢。

Then, how to put each php foreach value into each jquery ajax? (put every number via a jquery ajax, there will be 10+ ajax calls, and return all the data in one div). Thanks.

<script type="text/javascript">
jQuery(document).ready(function(){
    $.ajax({
        url: "process.php", 
        dataType: "html",
        type: 'POST', 
        data: "items=<?php echo $count; ?>", //each $count value in each ajax
        success: function(data){
            $("#result").html(data);
        }
    });
});
</script>
<div id="result"></div>


推荐答案

如果你真的想要一个独特的ajax调用每个$计数值,数据在#result div中返回:

If you really want a unique ajax call for each $count value, with the data returning in the #result div:

<script type="text/javascript">
    $(document).ready(function(){

<?php
foreach($data['data'] as $data){
    $count = $data['number'];
    ?>
      $.ajax({
        url: "process.php", 
        dataType: "html",
        type: 'POST', 
        data: "items=<?php echo $count; ?>",
        success: function(data){
            $("#result").append(data);
        }
      });
    <?php
}
?>

    });
</script>
<div id="result"></div>

但是,我强烈建议将值作为一个数组传递,只有一个ajax调用: p>

However, I would highly recommend passing the values as an array and having only one ajax call:

$count = array();
foreach($data['data'] as $data){
    $count[] = $data['number'];
}
$datacount = implode('-',$count);
?>

<script type="text/javascript">
    $(document).ready(function(){
      $.ajax({
        url: "process.php", 
        dataType: "html",
        type: 'POST', 
        data: "items=<?php echo $datacount; ?>",
        success: function(data){
            $("#result").append(data);
        }
      });
    });
</script>
<div id="result"></div>

在process.php的服务器端,可以 explode(' ',$ _ POST ['items'])然后通过它们进行foreach。

On the server side in process.php, you can explode('-',$_POST['items']) and then foreach through them.

这是完成它的另一种方法。它可以是json_encoded或许多其他方式。

This is just one other way to accomplish it... It could be json_encoded or many other ways.

这篇关于如何把每个PHP的foreach值到每个jquery ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆