Pass array to php page using JQUERY

How to pass array to php using jquery
function savedata()
{
var val = [];
$(’:checkbox:checked’&& ‘:checkbox:enabled’).each(function(i)
{
val[i] = $(this).val();

     $.ajax({
            type:"POST",
            url:"display.php",
            data:{"arr":val[i]};                          
            success: function(data)
            {                   
               
            }
     });
       
});
       }
      </script>

This should set the problem straight.

checkboxes.php:
[php]

<form" id=“form”>
I have a bike

I have a car

[/php]

post.php:
[php]<?php
if(!empty($_POST[‘vehicle’])) {
foreach($_POST[‘vehicle’] as $check) {
echo $check;
}
}
?>[/php]

When I first saw your selector, I figured it was a mistake and you just didn’t know where to attach it to. So, the code above will execute when the Submit button is clicked. But if you’d prefer to have the code execute upon clicking the checkbox, you can do that too.

But I don’t think it’s as complicated as you’re making it. If you replace this line:

$("form#form #submit").click(function(event) {

With this line:

$("form#form .checkbox").click(function(event) { (after assigning class=“checkbox” to all your checkboxes)
Then the $_POST will update not just when the checkbox is selected, but also when it’s deselected.

Hope this helped.

Sponsor our Newsletter | Privacy Policy | Terms of Service