jQueryeach()结果数组到php到数据库

发布于 2024-11-04 07:40:16 字数 1474 浏览 2 评论 0原文

基本上我正在做的是制作一种邀请系统,用户单击用户,他们进入一个列表,一切正常,我可以使用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: [电子邮件受保护] 通过:phpfreaklogin

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

Basically what I am doing is making a sort of invitation system, the user clicks on users and they go into a list, that all works, I can get the ids of them using each() but I need to pass it through jQuery Ajax to php to send it to the database for notifications. This is basically what I have:

$(".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. 
                    }
            });
        }
    }
});

if you want to see what I'm trying to accomplish just go here:

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

usr: [email protected]
pass: phpfreaklogin

It's under Group Video. (You should be automatically directed there once logged in)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熊抱啵儿 2024-11-11 07:40:16

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

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

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

You might be able to use jQuery.serialize to bundle up all of your form data. Also, jQuery.post is a nice shortcut for doing a POST request with jQuery.ajax.

A rough example might look this:

$.post( '/my-ajax-service.php', 
        $('#MyForm').serialize(), 
        function(data, txtStatus, jqXHR) {
            //Do stuff
});
沩ん囻菔务 2024-11-11 07:40:16

这是一种可能性

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) {}
    });
}

});

Here is one possibility

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) {}
    });
}

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文