Problem with jQuery Ajax and PHP

1

I am trying to make a record in the BD without recharging the page. The record is saved correctly, but when I want to capture the answer the PHP document is displayed.

Code js

$('#btnagregarO').click(function(){
var obs = $("#txtob").val();
var cliente = $("#txtcli").val();  

var datos = "obs="+obs+"&cliente="+cliente;      

$.ajax({
        type        : 'POST',
        url         : 'Usuario.php',
        data        : datos,            
        success:function(resp){                    
            alert(resp); 
        }                
    });
event.preventDefault();

});

PHP File:

$u->conexion();


$sql = "insert observacion values(NULL,'$Obs',$N,'$usu','$fecha')";     
$sentencia = $u->m->query($sql);                             
if($u->m->affected_rows > 0){

    echo "Se pudo registrar";                
}else{

    echo "No se pudo registrar";
} 
$u->desconexion();

Hopefully help me that first time using ajax and I do not know what can be wrong.

    
asked by Pamela Gutierrez 20.09.2017 в 20:19
source

1 answer

2

Well look, you should use the html () function that captures the php response in the following way.

$('#btnagregarO').click(function(){
var obs = $("#txtob").val();
var cliente = $("#txtcli").val();  

var datos = "obs="+obs+"&cliente="+cliente;      

$.ajax({
        type        : 'POST',
        url         : 'Usuario.php',
        data        : datos,            
        success: function(data){                    
            $(".resul-post-edit").html(data);              
    });
event.preventDefault();

to capture the results you put in a div like this

<div class="resul-post-edit"></div>

corrected

    
answered by 20.09.2017 / 20:37
source