0

i have write code by ajax to sent name and roles by ajax, the name send success, but checkbox not send when choose more than 2 roles.

ajax code

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

        }

   } 

and i get it on other page like this

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

the problem is

Invalid argument supplied for foreach()
5
  • 1
    $selected is missing or empty..can't find it anywhere else in your code? Commented Mar 5, 2013 at 23:44
  • @Oli i get it on php page like this $selected = $_POST["tper"]; Commented Mar 5, 2013 at 23:54
  • add an alert(pmeters) after the 'var pmeters' and check if all data is filled in Commented Mar 6, 2013 at 0:01
  • its like this troles_Name=admin&tper=1&myPage=1&tID=&tMode=ADD but tper we must take more than one value Commented Mar 6, 2013 at 0:05
  • on my code here "&tper=" + encodeURI( document.getElementById("per").value) + we must get more than one value how can do that Commented Mar 6, 2013 at 0:07

2 Answers 2

0

Pass all checkboxes values as an array in Javascript

This deals with your problem of selecting values and passing them to 'tper'

Sign up to request clarification or add additional context in comments.

Comments

0

Are the checkboxs defined like an array? For example:

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

6 Comments

how can i defined it by array on my code and get it on php page
<input type="checkbox" name="check[]" value="val"> <input type="checkbox" name="check[]" value="val2">
<input type="checkbox" name="selected[]">
i will defined <input type="checkbox" name="per[]" id="per" value="<?=$objResult['permission_id']; ?>" /> <?=$objResult['permission_name']; ?> and get same problem
If you do it without ajax, but with a classic form, it works?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.