I would like to ask for help, it's the first time I've done something like that, I'm still trying to learn. The question is, I have a page index.php, another page validation.php, and a file of JavaScript functions and another that serves to send the user to the registration page. I want to validate the same functions that I have done in the client, in the server with PHP, but it always happens to me, and it does not redirect me to the index again when something is not right. For example, I have the following function worked correctly in JS
function comprobarNombre(){
var nombre = document.getElementById('inputNombre');
if (nombre.value.length < 3 || nombre.value.length > 20) {
var alertNombre= document.getElementById('alertNombre');
alertNombre.style.visibility = "visible";
return 1;
} else {
var alertNombre = document.getElementById('alertNombre');
alertNombre.style.visibility = "hidden";
return 0;
}
}
<div class="form-group">
<label class="col-form-label" for="inputNombre">Nombre</label>
<input type="text" class="form-control" placeholder="Nombre" name="nombre" id="inputNombre"
onchange="comprobarNombre()" required>
</div>
In the index itself at the beginning, I have the session open with session_start (); In my PHP validation file, I have the following
<?php
session_start();
if(isset($_POST['enviar'])){
$_SESSION['nom'] = $_REQUEST['nombre'];
}
if(isset($_POST['enviar'])){
$nombre = $_SESSION['nom'];
}
function validarNombre( $nombre){
if (!(strlen($nombre)>5 && strlen($nombre)<20 || $nombre==""){
header("Location: index.php");
} else {
header("Location: registrado.php");
}
}
I do not understand how, it is allowing me to access the registration page if I leave the field empty. Someone, please can you help me, I have to do this treatment through SESSION, but as you can see, I do not understand why it does not work. Thanks