Evaluate variable received with request to the server with ajax

0

Good afternoon, I have the following 3 codes.

Using the javascript code with ajax made the request and it is done correctly, this is the login of a user, in case of being correct the data says "Welcome" otherwise "There are no registered users with this data ".

My doubt would be is it possible to evaluate what the Ajax method returns to you? so that in case that the login session is correct, the page that is the index is displayed, and if it is wrong, the login of the user is kept in that same login window. How can I interact with "data" which is what returns the request to the server with ajax.

For your attention, thank you.

Javascript code

$(document).ready(function() {

	$('#boton').click(function(event) {
		event.preventDefault();
		var1 = $('#usuario').val();
		var2 = $('#pass').val();

		$.ajax({
			url: 'Controlador/login.php',
			type: 'POST',
			data: {var1, var2},
			beforeSend:function(){
				console.log("Se esta procesando tu peticion");
			}
		})
		.done(function(data) {
			console.log("success");
			alert(data);
		})
		.fail(function() {
			console.log("error");
		})
		.always(function() {
			console.log("complete");
		});
	});
	

	
	$('#loguearse').click(function(event) {	
		$('#ve').load('login.html');
		$('#general').hide();		
	});
	
});

PHP Code

<?php

	require_once('../Modelo/class.conexion.php');
	require_once('../Modelo/class.consultas.php');

	$mensaje = null;

	$usuario = $_POST['var1'];
	$pass = $_POST['var2'];

	if(strlen($usuario) > 0 && strlen($pass) > 0){
		$consultas = new Consultas();
		$mensaje = $consultas->verificarUsuario($usuario, $pass);	
	}else{
		echo "Por favor rellena ambos campos";
	}

	echo $mensaje;

?>

User verification method

public function verificarUsuario($nombre_usuario,$password_usuario){
				$modelo = new Conexion();
				$conexion = $modelo->get_conexion();
				$sql = "select nombre_usuario, password_usuario
						from usuarios
						where nombre_usuario = :nombre_usuario 
						and password_usuario = :password_usuario";
				$statement = $conexion->prepare($sql);				
				$statement->bindParam(':nombre_usuario', $nombre_usuario);
				$statement->bindParam(':password_usuario', $password_usuario);
				if(!$statement){
					return "No hay usuario registrado con esos datos";
				}else{
					$statement->execute();
					if($statement->rowCount() > 0){				 		
						return "Bienvenido";
					}else{
						return "no hay usuarios con registrados con esos datos";
					}	
				}		
    
asked by antonio291093 25.06.2017 в 00:59
source

1 answer

1

Your data variable is the one that brings the response of the server, in fact here you bring that information:

.done(function(data) {
            console.log("success");
            alert(data);
        })

For example you could do:

.done(function(data) {
            if (data == "Bienvenido")
                document.location.href = "index.php";
            else 
                alert(data);
        })

What I would recommend is to generate a more elaborate response from the server, that you can return a JSON with a Status and a Message or something like that.

Greetings

    
answered by 25.06.2017 / 01:25
source