doudun2565
2016-07-15 04:35
浏览 64

如何在PHP中使用AJAX从一个页面到另一个页面将选择框值作为数组传递?

var message = $("#send_message").val();

    var Teaminput = $("#sms_reminder_team").val();
    for (var i = 0; i <Teaminput.length; i++) 
    {
       var team=Teaminput[i];
    }

    var Memberinput = $("#sms_reminder_members").val();
    for (var i = 0; i <Memberinput.length; i++) 
    {
    var members=Memberinput[i];

  }

Get 2 varaibles as array members and team

var parameter = "message="+message+"&team="+team+"&members="+members;
  $.ajax({
  url: base_url+'ajaxfiles/dir_sendmessage',
  type: 'POST',
  data: parameter,
  success: function(data)
  {
    document.getElementById('check').innerHTML = data; 
  }
  });

How to send both array variables using AJAX from current page to "dir_sendmessage".

图片转代码服务由CSDN问答提供 功能建议

  var message = $(“#send_message”)。val(); 
 
 var Teaminput  = $(“#sms_reminder_team”)。val(); 
 for(var i = 0; i&lt; Teaminput.length; i ++)
 {
 var team = Teaminput [i]; 
} 
 \  n var Memberinput = $(“#sms_reminder_members”)。val(); 
 for(var i = 0; i&lt; Memberinput.length; i ++)
 {
 var members = Memberinput [i]; 
 \  n} 
   
 
 

获取2个变量作为数组成员和团队

  var parameter =“message =”+ message +  “&amp; team =”+ team +“&amp; members =”+ members; 
 $ .ajax({
 url:base_url +'ajaxfiles / dir_sendmessage',
 type:'POST',
 data:parameter,\  n success:function(data)
 {
 document.getElementById('check')。innerHTML = data; 
} 
}); 
   
 
 

如何使用当前页面的AJAX将两个数组变量发送到“dir_sendmessage”。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • duanliao2310 2016-07-15 04:45
    已采纳

    Change the below line

    var parameter = "message="+message+"&team="+team+"&members="+members;
    

    to

    var parameter = "message="+message+"&team="+JSON.stringify(team)+"&members="+JSON.stringify(members); 
    

    Edit: Modify like this too

    var team = [];
    var members = [];
    for (var i = 0; i <Teaminput.length; i++) 
    {
       team=Teaminput[i];
    }
    
    var Memberinput = $("#sms_reminder_members").val();
    for (var i = 0; i <Memberinput.length; i++) 
    {
       members=Memberinput[i];
    }
    

    Note: When you add var in each line in the loop, it will declare a new variable. You have to edit like the above code

    After update the code with the JSON.stringify() function, you will be able to get the value as an array in you PHP code

    Ajax will not directly pass Jquery array to PHP

    打赏 评论
  • dongxie9448 2016-07-15 04:46

    First of all ideally you should send in JSON format or use array.tostring() ( can avoid this )

    But if you have to send it as array you can try following:

    $.ajax({ 
    url: base_url+'ajaxfiles/dir_sendmessage', 
    type: 'POST',
     data: {team:team, members: members}, 
    success: function(data) { 
    document.getElementById('check').innerHTML = data; } });
    
    打赏 评论
  • dongyan4157 2016-07-15 05:05
    var message = $("#send_message").val();
        var teaminputt = $("#sms_reminder_team").val();
        team = new Array();
        members = new Array();
        for (var i = 0; i <teaminputt.length; i++) 
        {
           var team=teaminputt[i];
        }
    
        var memberinput = $("#sms_reminder_members").val();
        for (var i = 0; i <memberinput.length; i++) 
        {
        var members=memberinput[i];
    
        }
    
        var parameter = "message="+message+"&team="+team+"&members="+members;
        $.ajax({
            url: base_url+'ajaxfiles/dir_sendmessage',
            type: 'POST',
            data: parameter,
            success: function(data)
            {
            document.getElementById('check').innerHTML = data; 
            }
        });
    
    打赏 评论
  • duanqianwei2485 2016-07-26 06:28
    $("#msg-send-btn").click(function() {
          var message = $("#send_message").val();
          var optionsmembers = $('#sms_reminder_members option:selected');
          var members = $.map(optionsmembers ,function(option) {
          return option.value;
          });
    

    //---- Using $.map get all selcted data as a an Array.

    var postData = {
                      message,
                      members
                   }
    

    //---- For Avoid Json-Stringfy.

     $.ajax({
      url: base_url+'ajaxfiles/dir_sendmessage.php',
      type: 'POST',
      data:{myData:postData},
    

    //----- And It's Work Perfectly.

    打赏 评论

相关推荐 更多相似问题