【问题标题】:jQuery each() results to array to php to databasejQuery each() 结果到数组到 php 到数据库
【发布时间】:2011-08-15 17:30:36
【问题描述】:

基本上我正在做的是制作一种邀请系统,用户点击用户,他们进入一个列表,一切正常,我可以使用 each() 获取他们的 ID,但我需要通过它jQuery Ajax 到 php 将其发送到数据库以获取通知。这基本上就是我所拥有的:

$(".group-video-create").click(function(){
    var url = $(".group-input-url").val();                                      
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    var checked_url = url.match(exp,"<a href='$1'>$1</a>"); 
    if(checked_url)
    {   
        $("#group-input-names li").each(function(){ // This is the relevant code
        var user_id = $(this).attr("id");           // Here too
        });                                         // & here

        if(user_id)
        {

            $.ajax({
                type: "POST",
                //url: "",
                //data: "", //this would be an array of all of the ids, (could be 1, could be 100).
                cache: false,
                success: function(html){
                    ///Once all invitations have been sent to the database it would then load a new div and hide the previous one. 
                    }
            });
        }
    }
});

如果你想看看我想要完成什么,请到这里:

http://www.kithell.com/#/video

usr: PhpFreak@yahoo.com 通过:phpfreaklogin

它在群组视频下。 (您应该在登录后自动定向到那里)

【问题讨论】:

  • jQuery 拼写为“jQuery”,而不是“Jquery”。

标签: php jquery arrays each


【解决方案1】:

您也许可以使用jQuery.serialize 来捆绑您的所有表单数据。此外,jQuery.post 是使用 jQuery.ajax 进行 POST 请求的一个不错的快捷方式。

一个粗略的例子可能是这样的:

$.post( '/my-ajax-service.php', 
        $('#MyForm').serialize(), 
        function(data, txtStatus, jqXHR) {
            //Do stuff
});

【讨论】:

    【解决方案2】:

    这是一种可能性

    http://jsfiddle.net/nickywaites/9GZ2e/

    $(function() {
    
    //I would use jQuery Map to build Array
    //http://api.jquery.com/map/
    var ids = $("#group-input-names li").map(function() {
        return $(this).attr("id");
    }).get(); //Get Required to convert to regular javascript array
    
    console.log(ids);
    
    var invites = {}
    invites.users = ids;
    
    //Then use JSON.stringify() to pass array to server
    //https://github.com/douglascrockford/JSON-js
    if (ids.length > 0) {
        $.ajax({
            type: "POST",
            //url: "",
            data: JSON.stringify(invites),
            cache: false,
            success: function(html) {}
        });
    }
    
    });
    

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 1970-01-01
      • 2019-03-13
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      相关资源
      最近更新 更多