如何通过复选框发送多个值

时间:2013-03-05 23:40:10

标签: php ajax

我已经通过ajax编写代码来发送ajax的名称和角色,名称发送成功,但是当选择2个以上的角色时,复选框不会发送。

ajax代码

   var HttPRequest = false;

   function doCallAjax(Mode,Page,ID) {
      HttPRequest = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         HttPRequest = new XMLHttpRequest();
         if (HttPRequest.overrideMimeType) {
            HttPRequest.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      } 

      if (!HttPRequest) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }

      var url = 'AjaxRolesPermRecord.php';
      var pmeters = "troles_Name=" + encodeURI( document.getElementById("roles_Name").value) +
                    "&tper=" + encodeURI( document.getElementById("per").value) +
                    '&myPage='+Page +
                    "&tID=" + ID +
                    "&tMode=" + Mode;

        HttPRequest.open('POST',url,true);

        HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        HttPRequest.setRequestHeader("Content-length", pmeters.length);
        HttPRequest.setRequestHeader("Connection", "close");
        HttPRequest.send(pmeters);


        HttPRequest.onreadystatechange = function()
        {

             if(HttPRequest.readyState == 3)  // Loading Request
              {
               document.getElementById("mySpan").innerHTML = "load ...";
              }

             if(HttPRequest.readyState == 4) // Return Request
              {
               document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
              }

        }

   } 

我在其他页面上得到它

foreach($selected as $key=>$val) 
{  
    $SqlInsertIntotable = mysql_query("INSERT INTO permissions_roles (id,permission_id,role_id) 
                                       value ('','".$val."','".$RoleID."')"); 
}

问题是

Invalid argument supplied for foreach()

2 个答案:

答案 0 :(得分:0)

复选框是否定义为数组? 例如:

 <input type="checkbox" name="selected[]">

答案 1 :(得分:0)

Pass all checkboxes values as an array in Javascript

这会解决您选择值并将其传递给'tper'

的问题