post checkbox values to a php file

10,145

Solution 1

I assume you wanted to achieve this through using jQuery ajax call to post to php

Html

<form>
    <input type="checkbox" name="id[]" id="id" value='1'>
    <input type="checkbox" name="id[]" id="id" value='2'>
    <input type="checkbox" name="id[]" id="id" value='3'>
    <input type="button" name="DELETE" id="DELETE">
</form>

Javascript with jQuery

var selected_values = $("input[name='id[]']:checked");

Directly post to delete.php

<?php
    // $_POST['id'] will return an array.
    $selected_values = $_POST['id'];
?>

Solution 2

HTML

<form>
    <input type="checkbox" name="id" value='1'>
    <input type="checkbox" name="id" value='2'>
    <input type="checkbox" name="id" value='3'>
    <input type="button" name="DELETE" id="DELETE">
</form>

JQUERY

<script>
$(document).ready(function(){

    $("#delete").click(function(){

        var data = $('input:checkbox:checked').map(function(){
             return this.value;
        }).get();


        var dataString = "imgList="+ data;

       $.post('delete.php',dataString,function(theResponse){
       //// Check theResponse
       });               


   });      
});
</script>

PHP

$imgList = $_REQUEST['imgList'];

$i = 0;

$token = strtok($imgList, ","); 

$imgArray = array();

while ($token != false){

    $imgArray[$i] = (string)$token;

    $token = strtok(",");

    $i++;

} 
Share:
10,145
Andrei M.
Author by

Andrei M.

Hi, I'm Andy, an experienced PHP developer with over 7 years experience in this field. I like to build custom solutions from scratch matching exact requirements of the project and seeking to be perfect and bulletproof. I've added few of my skills aquired on those years, working for multiple companies, freelancing and personal projects. Highly experienced with PHP &amp; MySQL(About 6 years). I will work in the style needed(procedural or oop), building from scratch or using CMS/ Framework. Experienced with various APIs(Amazon Product API, Paypal, MailChimp, Sengrid Google API, etc). Experienced with HTML5 + CSS, Bootstrap. Experienced with jQuery(+various plugins like datatables) &amp; Javascript . Experienced with different CMS &amp; Software (Wordpress, Joomla, phpBB, Discourse, Zencart, Magento). Experienced Linux (I configured various web envroiments on various linux distros like Centos 5/6 , Ubuntu, Debian etc.). Experience in using GIT, GitHub or BitBucket.

Updated on June 04, 2022

Comments

  • Andrei M.
    Andrei M. almost 2 years

    I'm trying to write a script to post with jQuery checkboxes ids that are checked(for php deletion script).

    I have something like this

       <form>
        <input type="checkbox" name="id" id="id" value='1'>
        <input type="checkbox" name="id" id="id" value='2'>
        <input type="checkbox" name="id" id="id" value='3'>
        <input type="button" name="DELETE" id="DELETE">
         </form>
    

    So I want to post those values(ids) to a php file delete.php, how can I achieve this?

    • Mithun Satheesh
      Mithun Satheesh about 11 years
      multiple dom elements in a page are not supposed to have same id attribute