lundi 30 juillet 2018

How to send the value of Radio Button with Ajax to PHP when selecting

my problem is when I sent my variables through ajax to my .php. The variable 'data.estado' marks me an error once it returns, I already could bring the element that selects and obtain its value but there is an error in my return of ajax data. I do not know what it is. A help please and thanks for giving me a return to my question hehehe :).

My ajax code and controlling the selected radius:

var ComprobarSeleccion = false;

$('input[type=radio][name=radioStacked]').change(function(){

    ComprobarSeleccion = true;

    $.ajax({
         url: "cuestionario.php",
         type: "POST",
         data:{ valor:this.value, verificar:ComprobarSeleccion},
         dataType:"jsonp",
         jsonp:"jsoncallback",
         contentType:'application/json; charset=utf-8',
         crossDomain: true,
         cache: false,
         success:function($data){
            //unselect(this);
             //console.log($data.estado);
             if($data.estado=="seleccionado"){
                alert("Exito");
             }

             console.log($data.estado);

         },
         error: function($data){
            alert( "Error en la petición: " +  JSON.stringify($data));
         }
    });

});

My PHP code where I try to catch the value of the radio button:

<?php
        session_start();
        if(isset($_SESSION['sesion_iniciada']) == true ){

                $id_alumno = $_SESSION['alumno'] ;

                $datos = array();
                $a = false;    
                if(isset($_GET['verificar']) && !empty($_GET['verificar'])){
                    if($_GET['verificar']){
                       $valor = $_GET['valor'];
                       $a = true;

                       $datos["estado"] = "seleccionado";
                       //var_dump($datos);
                    }else{
                       $datos["estado"] = "error123";  
                       $a = true;

                    }
                 }
                 //var_dump($datos);

                 if($a == true){
                     $resultadoJson = json_encode($datos);

                     //var_dump($resultadoJson);
                     echo $_GET['jsoncallback'] . '(' . $resultadoJson . ');'; 
                 }        
    ?>

The truth is I do not know if I am taking the most appropriate and effective way, please accept suggestions, indications and corrections. Thank you.

My error in the ajax request:

Error en la petición: {"readyState":4,"status":200,"statusText":"load"}

My error in the console:

Uncaught SyntaxError: Unexpected token < 

What the data variable prints on the console when my ajax arrives:

jQuery331004199455651714534_1532107113667({"estado":"seleccionado"});

And it marks me an error in my HTML tag, it highlights it with red color and with an 'x' this one.




Aucun commentaire:

Enregistrer un commentaire