Met AJAX will ik checkbox waardes in een array sturen naar php.
<label>1 <input type="checkbox" class="ids" name="ids[]" value="1"></label>
<label>2 <input type="checkbox" class="ids" name="ids[]" value="2"></label>
<label>3 <input type="checkbox" class="ids" name="ids[]" value="3"></label>
<label>4 <input type="checkbox" class="ids" name="ids[]" value="4"></label>
<label>5 <input type="checkbox" class="ids" name="ids[]" value="5"></label>
<label>6 <input type="checkbox" class="ids" name="ids[]" value="6"></label>
<button id="submit">Submit</button>
<div id="response"></div>
<script>
$(document).ready(function(){
$('#submit').click(function() {
var input = $('.ids:checked').serialize();
$.ajax({
url: "/testajax.php",
type: "GET",
data: ({what:input}),
success: function(data) {
$('#response').html(data);
}
});
});
});
</script>
<?php
$arr = $_GET['what'];
var_dump ($arr);
?>
De uitkomst isstring(11) "ids%5B%5D=6"
Doe ik de code zoals hieronder dan krijg ik de array wat ik wil
<script>
$(document).ready(function(){
$('#submit').click(function() {
//var input = $('.ids:checked').serialize();
$.ajax({
url: "/testajax.php",
type: "GET",
data: $('.ids:checked').serialize(),
success: function(data) {
$('#response').html(data);
}
});
});
});
</script>
<?php
$arr = $_GET['ids'];
var_dump ($arr);
?>
De uitkomst array(1) { [0]=> string(1) "6" }
Waarom word met de bovenste code een string terug gegeven?