doubt with answer with ajax

0

We have the following validation with ajax, the idea is to perform a validation of a field with a table. through the following function I send the parameters

function val_tipo_venta(c_almacen,c_tipo_venta){
    ajax=nuevoAjax();
    ajax.open('POST','ajax_validar_alm.php',true);

    ajax.onreadystatechange=function(){
        if (ajax.readyState==4){
            console.log(ajax.responseText);
        }
    }
    ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    ajax.send('&c_almacen='+c_almacen+'&c_tipo_venta='+c_tipo_venta);
    return false;        
}

File that performs the query. ajax_validar_alm.php

$c_almacen=$_POST['c_almacen'];
$tipo_venta=$_POST['c_tipo_venta'];
?>


<?php

$miret=0;
$sql = "select campo1 from tabla
         WHERE campo1= ".$c_almacen."
           AND camo2=".$c_tipo_venta."";
p_query($sql);
$registro = f_next_row($g_idq);
if( !$registro ){ return 0 }
?>

he makes a query and if there is no result he returns 0

then in another function I try to get the value of this function, but I always bring undefined

var sw_respuesta= val_tipo_venta(almacen,tipo_venta);
if(sw_respuesta==0){
    alert("El tipo de venta seleccionado, no esta relacionado al almacen,por favor  realiza la configuración en el maestro de tipos de venta.");
    return false;
} 

Is it okay this way or am I doing something wrong?

    
asked by Norbey Martinez 01.08.2018 в 19:24
source

1 answer

0

I think you are printing the return wrong, to see this you could do a console.log (sw_respuesta);

the other thing you could do is from your php return a json type and this would be something like that

return  ["status" => 1 , "mensaje" => "tu mensaje aqui"];

and from your ajax file you would take it as follows.

sw_respuestas.mensaje o sw_respuestas.status.

I hope it helps you.

    
answered by 01.08.2018 в 19:39