1

Checkbox array can not pass to php through ajax in case of FormData. Following script is working fine where FormData hasn't been used. So I assume problem is in while appending in FormData or passing from it. It returns invalid arguments supplied for foreach().

Checkbox is not part of form element, so

  <input type="checkbox" id="menu" name="menu[]" value="1">
  <input type="checkbox" id="menu" name="menu[]" value="2">
  <input type="checkbox" id="menu" name="menu[]" value="3">

  <form id="form">
  ...........
  </form>

Jquery and ajax

$(document).on('submit', '#form', function(e){
    e.preventDefault();
    var navid = [];
        $("[name='menu[]']:checked").each(function (i) {
        navid[i] = $(this).val();
    });
    if (navid.length === 0){ //tell you if the array is empty
        alert("Please Select atleast one checkbox");
    }
    else {
    var formData = new FormData(this);
    formData.append('navid', navid);
    $.ajax({
        type: 'POST',
        url: 'upload.php',
        data: formData,
        contentType: false,
        cache: false,
        processData:false,
        success: function(data){
            alert(data);
        }
    });
    }
});

PHP

foreach ($_POST["navid"] AS $key => $item){               
    $query1 =$con->prepare("INSERT INTO menu(cid, title, en_title) VALUES (:navid, :menuin, :menueng)");
    $query1->bindParam(':menunin',$_POST["menunin"][$key]);
    $query1->bindParam(':menueng',$_POST["menueng"][$key]);
    $query1->bindParam(':navid',$item);
    $query1->execute();
    echo 'Menu has inserted';     
}
4
  • your code is perfect. working fine. see in fiddle codepen.io/creativedev/pen/yEYped. See in network tab Commented Jun 2, 2018 at 5:00
  • not the issue, but shouldn't you prepare just once instead of having prepare() in the loop? Commented Jun 2, 2018 at 5:04
  • @BhumiShah, but it returns invalid arguments supplied for foreach() from php. Please see php code, is it OK ? Commented Jun 2, 2018 at 5:05
  • do a print_r($_POST['navid']); seems the array is not valid Commented Jun 2, 2018 at 5:07

1 Answer 1

1

Yes issue in your php code:

You are passing comma seperated values so first explode and use

$data = explode("," ,$_POST["navid"]);
foreach ($data AS $key => $item){
    $query1 =$con->prepare("INSERT INTO menu(cid, title, en_title) VALUES (:navid, :menuin, :menueng)");
    $query1->bindParam(':menunin',$_POST["menunin"][$key]);
    $query1->bindParam(':menueng',$_POST["menueng"][$key]);
    $query1->bindParam(':navid',$item);
    $query1->execute();
    echo 'Menu has inserted';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, after exploding, foreach clause is worked but following error is generated Only variables can be passed by reference at $query1->bindParam(':menunin',$_POST["menunin"][$key]); I tried a lot, but why this error is coming ?
Take $_POST["menunin"][$key] and $_POST["menueng"][$key] in variable and pass variable to bindparam value

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.