jQuery/PHP Foreach Ajax Request


jQuery/PHP Foreach Ajax Request

我试图在我的新网站代码中使用jQuery。目前,对于在页面上"添加"的每个div类,它循环遍历它们并向我的PHP脚本提交AJAX请求,然后更新数据库。这样做的问题是,如果用户创建了100个项目,将发送100个请求,这是不可接受的。我更喜欢一次性发送所有数据,所以如果有100个条目,PHP只需要循环遍历$_GET数组。

$( ".added" ).each(function() {
    var position = $(this).position();
    $.ajax({
        type: "GET",
        url: "index.php?action=save",
        data: {
            id: $(this).attr("id"),
            top: position.top,
            left: position.left,
            img: $(this).attr("image")
        },
        success: function (data) {
            //alert(data);
        }
    });
});     

任何解决方案,或者对于那些能给我正确方向的人,我将非常感激。

亲切的问候。

在检查了您的需求之后,最好创建一个数组来保存每个添加的类的信息。然后你可以将这个数组发送到服务器(这将有助于将数组转换为JSON,这是一个建议)

var position_array = [];
$('.added').each(function()
{
    //Get the position of the element       
    var position = $(this).position();
    var id = $(this).attr("id");
    var top = position.top;
    var left = position.left;
    var img = $(this).attr("image");
    var object = 
    {
        'id': id,
        'top': top,
        'left': left,
        'img': img
    }
    position_array.push(object);
}); 
$.ajax(
{
    type: "GET",
    url: "index.php?action=save",
    data: 
    {
        add_array: position_array
    },
    success: function (data) 
    {
            //alert(data);
    }
});

您可以使用POST方法而不是GET,这将使您在向PHP传递参数时更加自由。之后,只需通过data字段传递div数组。

$( "#submitDivs" ).on('click',function() {

    var divsData = [];
    $( ".added" ).each(function() {
        var position = $(this).position();
        divsData[$(this).attr("id")] = {
            id: $(this).attr("id"),
            top: position.top,
            left: position.left,
            img: $(this).attr("image")
        };
    });
    $.ajax({
        type: "POST",
        url: "index.php?action=save",
        data: { divs: divsData },
        success: function (data) {
            //alert(data);
        }
    });
});

当ID为' submitdiv '的元素被点击时,这个函数将被触发。之后,在PHP中,您可以执行以下操作来遍历div:

$divs = json_decode($_POST['divs']);
foreach($divs as $div){
    echo $div['id'];
}
Simple Way to do this .. just set a timer javascript for your client to do another request().. (only client have charge cpu and server do just one request to 3 sec for one client)
For good protection you do this in server also ..
var urls = [];
var timer = now();
onclick(function(){
    add_url_in_urls[]($(this).href());
]);
setInterval(function(){
  dooooo();
},3000);
function dooooo(){
timer = renow();
    if (timer > now()){
        $( ".added" ).each(function() { // added links or list url in array javascript //
            var position = $(this).position();
            $.ajax({
                type: "GET",
                url: "index.php?action=save",
                data: {
                    id: $(this).attr("id"),
                    top: position.top,
                    left: position.left,
                    img: $(this).attr("image")
                },
               success: function (data) {
              //alert(data);
           }
        });
    });     
 }
}
(toujours modérer ses actions javascript en functions réutilisables ...)