简体   繁体   English

将.php数组传递到JavaScript代码jquery / ajax中

[英]passing .php array into a javascript code jquery/ajax

At the moment i have this piece of javascript code: 目前,我有这段JavaScript代码:

//Display custom confirm box and delete multiple message

$(document).ready(function () {
    $(".delete_button-multiple").click(function () {
        //Get message id as variable
        var id = $(this).attr("id");
        var dataString = 'id=' + id;
        var parent = $(this).parent();
        //Display custom Confirm box  
        jConfirm('Are you sure you want to delete this message?', '', function (r) {
            if (r == true) { //initiate delete message if agreed

                $.ajax({
                    type: "POST",
                    url: "delete-mail_ajax.php",
                    data: dataString,
                    cache: false,

                    success: function () {
                        window.location = "mail_inbox.php";
                    }
                });

                return false;
            }
        });

    });
});


delete-mail_ajax.php:

if($_POST['id'])
{
$id=$_POST['id'];
$id = mysql_escape_String($id);
$sql = "delete FROM mail WHERE mail_id='$id'";
mysql_query( $sql);
} 

This is a working code for deleting only one mail item. 这是仅删除一个邮件项目的工作代码。 I wrote the following code to delete multiple messages from checkboxes: 我编写了以下代码,以从复选框中删除多条消息:

        //Multiple delete mail
        if(!empty($_POST['message'])){ 
        $list_mail = $_POST['message'];
        foreach ($list_mail as $messageID){    
        $sql = "delete FROM mail WHERE mail_id='$messageID'";
        mysql_query($sql);
        //deletion complete, refresh the page
        header("Location: mail_inbox.php");
        }
        }//end delete multiple 

The difficulty i'm having is changing the working code above to incorporate the multiple selection, and deletion, of selected mails. 我遇到的困难是更改上面的工作代码,以合并所选邮件的多次选择和删除。

Any help on this issue would be appreciated 在这个问题上的任何帮助,将不胜感激

-Callum -Callum

Assuming you're using checkboxes, your code would look something like: 假设您正在使用复选框,那么代码将类似于:

var messages = new Array();
$("input[name='mail_items[]']:checked").each(function() {
   messages.push($(this).val());
});

$.ajax({
   type: "POST",
   url: "delete-mail_ajax.php",
   data: { message: messages }  //jQuery should translate this to an array that PHP should understand
   cache: false,
   ...
});

You may need to json_decode the input to the $_POST['message'] variable, but I'd do a var_dump() on the stuff first just to make sure what PHP is doing in the background. 您可能需要将输入的json_decode解码到$ _POST ['message']变量中,但是我首先会在东西上执行var_dump()只是为了确保PHP在后台执行操作。 Can't check at the moment, sorry. 目前无法检查,抱歉。

I guess you have trouble submitting the form via Ajax? 我想您在通过Ajax提交表单时遇到麻烦? There is a neat plugin that does that for you: http://jquery.malsup.com/form/ 有一个简洁的插件可以帮助您: http : //jquery.malsup.com/form/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM