I make this query from jQuery AJAX, because I'm doing a PHP, AJAX, JQUERY test, The issue is that I try to make a request to the server to send data from the user fields and password but when sending the data, I'm always going to error instead of success, in $ .ajax (), I do not know what happens here?
//Codigo jQuery 3.3.1
$('#send').click(function(){
$.ajax({
method:'post',
url:'login.php',
datatype: 'html',
data: user : {$('#user').val()},
success : function(response){
$('.alertArea p').text(response);
},
error : $('.alertArea p').text("Hubo un error al enviar los datos.!");
});
});
/*Codigo CSS */
*{
margin: 0;
padding: 0;
}
input{
padding: 10px;
}
h2{
font-family: "trebuchet ms";
}
.alertArea{
display: none;
}
.alertArea p{
background: #333;
}
<!-- Codigo HTML -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="stl.css">
<title>Formulario de imagenes</title>
</head>
<body>
<form method="post" align="center" id="form">
<h2>Inicie sesión</h2>
<hr>
<br>
<div class="alertArea">
<p></p>
</div>
<input type="text" placeholder="Nombre de usuario" name="user" id="user">
<input type="password" placeholder="Contraseña" name="pass">
<button id="send">Enviar</button>
</form>
<script src="jquery/jquery-3.3.1.min.js"></script>
<script src="js.js"></script>
</body>
</html>
<?php
include('conexion.php');
$usuario = $_POST['user'];
$contrasena = $_POST['pass'];
echo "Los datos son : 'Usuario : $usuario' , 'Contrasena : $contrasena'";?>
I add that the button being inside the form reloads the page, but when I put it out of the form I do not recharge the page, and even though I removed the action="", I really do not want to recharge the page as well, I guess that's the problem.