How can I not pass certain data from js to php:
The structure of some radios buttons
<input class="rad" name="1" id="106" value="0" type="radio">
<input class="rad" name="1" id="45" value="0" type="radio">
<input class="rad" name="2" id="12" value="0" type="radio">
<input class="rad" name="2" id="23" value="0" type="radio">
The JavaScript code:
function guardar(){
//Declaramos un objeto
var obj = {};
//iteramos todo lo que tenga clase rad
$('.rad').each(function(){
//Validamos si está chequeado
if( $(this).is(':checked') ){
//sacamos el heat en el que se encuentre
var heat = $(this).attr('name');
//creamos un nodo con el nombre del heat
obj[ heat ] = {};
//Agregamos la información al nodo con el nombre del heat
obj[ heat ]['id'] = $(this).attr('id');
// obj[ heat ]['value'] = $(this).val();
}
});
console.log('Esto es lo que se enviará al PHP');
console.log(obj);
//abrimos la petición AJAX y no le parseamos ni nada, así pasamos el objeto al nodo "data"
$.ajax({
url: 'decode.php',
method: 'GET',
data: obj,
success:function(response){
console.log(response);
},
error: function(e){
console.log('Error! El error está en que esta parte no está conectada a tu Back-end');
}
})
In PHP :
<?php
var_dump( $_REQUEST );
$heat1_id = $_REQUEST['heat1']['id'];
echo $heat1_id;
?>
Performing a test returns the following:
Javascript
PHP
array(0) { }
Many thanks to: @TO. Cedano for the orientation
and the last code you used was from @Alberto Siurob