I'm just coming back to web developing. In my first steps, I'm working on a basic log in which is turning me crazy. Here we go:
Js code just react to the login button, sending id and password.
PHP code checks the data received on the ddbb and returns a code error if there is a mismatch, or a URL to redirect if the login was successfully.
Js get the answer from php and then displays a message to the user or just redirects to the personal area.
Despite the rudimentary off the code and the design mistakes ( just trying to make it run), many times it works successfully, but many other it didn't get more than a 1 (server connection ok) on the onreadystatechange. So I need to ask, someone know what could it be?
Why onreadystatechange keeps it stuck on first step sometimes ? What am I missing?
Thanks a lot.
(once I get this one working, I will try again with other tools, minding jQuery instead basic js and jwt instead sessions, but this will be next one. I'll accept suggestions )
JavaScript code:
var boton = document.getElementById("boton");
boton.addEventListener('click', validarLogIn);
function validarLogIn()
{
var nombre = document.getElementById("nombre");
var contrase?�a = document.getElementById("contrase?�a");
var parametros="Nombre="+nombre.value+"&Contrase?�a="+contrase?�a.value;
http_request = new XMLHttpRequest();
http_request.onreadystatechange = resultadoLogIn;
http_request.open("POST", "../php/validarLogin.php");
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.send(parametros);
}
function resultadoLogIn()
{
var respuesta=http_request.responseText;
if (http_request.readyState == 4){
if (http_request.status == 200){
if(respuesta==1){
alert("Contrase?�a incorrecta.");
}else if(respuesta==2){
alert("Usuario no encontrado.");
}else{
window.location=respuesta;
}
}else{
alert('Hubo problemas con la petici??n.');
}
}
}
PHP code
$nombre =$_POST['Nombre'];
$contrase?�a =$_POST ['Contrase?�a'];
require_once 'DatosBBDD.php';
$consultaBbdd="SELECT ID,CONTRASE?�A FROM usuarios WHERE ID ='".$nombre."'";
$urlRedireccion="../php/vistaGeneral.php";
$link = mysqli_connect($ip,$usuarioBbdd,$contrasenaBbdd);
mysqli_select_db($link, $nombreBbdd);
$result = mysqli_query($link,$consultaBbdd);
mysqli_data_seek ($result, 0);
$extraido= mysqli_fetch_array($result);
mysqli_close($link);
$nombreV=false;
$contrase?�aV=false;
if(isset($extraido['ID'])&&isset($extraido['CONTRASE?�A'])){
if ($extraido['ID']==$nombre){$nombreV=true;}
if ($extraido['CONTRASE?�A']==$contrase?�a){$contrase?�aV=true;}
}
if($nombreV){
if($contrase?�aV){
session_start();
$_SESSION['idUsuario']=$nombre;
echo $urlRedireccion;
}else{echo "1";}
}else{echo "2";}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
React is currently the leader in JavaScript UI frameworks. First, the Facebook developers started working on this to make their job easier. An app called Facebook Ads grew very quickly, which meant complex management and support. As a result, the team began to create a structure that would help them with efficiency. They had an early prototype before 2011, and two years later, the framework was open source and available to the public. It is currently used by many business giants: AirBNB, PayPal, Netflix, etc.
https://reactjs.org/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.