简体   繁体   中英

Javascript inside else statement isn't working. Message doesn't appear

I'm making a basic login for a php website, the function is really simple if checks if the data send using POST is in a MySQL table and if it's correct it would allow the user to proceed it the data is incorrect, it should show a message saying credentials are incorrect:

<?php
   include("required/datos.php");
   session_start();

   if ($_SERVER["REQUEST_METHOD"] == "POST") {

      $mypassword = mysqli_real_escape_string($db,$_POST['password']);
      $grupo = mysqli_real_escape_string($db,$_POST['grupo']); 

      $sql = "SELECT * FROM usuarios WHERE BINARY password = '$mypassword' and grupo = '$grupo'";
      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);


      $count = mysqli_num_rows($result);


      if ($count == 1) {
         $_SESSION['grupo'] = $grupo;
         $_SESSION['autorizado'] = TRUE;
         header('location: horario.php');
      }
   } else {
      $message = "Credenciales incorrectas";
      echo "<script type='text/javascript'>alert('$message');</script>";
      header('Refresh: 0; URL=index.php');
   }
?>

The if statement is working so far, and even the else but the message isn't showing out at all, I'm trying to look where exactly is the problem but I can't found it.

Try this code. Your brackets are in the wrong position.

<?php
include("required/datos.php");
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $mypassword = mysqli_real_escape_string($db,$_POST['password']);
   $grupo = mysqli_real_escape_string($db,$_POST['grupo']); 

   $sql = "SELECT * FROM usuarios WHERE BINARY password = '$mypassword' and grupo = '$grupo'";
   $result = mysqli_query($db,$sql);
   $row = mysqli_fetch_array($result,MYSQLI_ASSOC);


   $count = mysqli_num_rows($result);


   if ($count == 1) {
      $_SESSION['grupo'] = $grupo;
      $_SESSION['autorizado'] = TRUE;
      header('location: horario.php');
   } else {
      $message = "Credenciales incorrectas";
      echo "<script type='text/javascript'>alert('$message');</script>";
      header('Refresh: 0; URL=index.php');
   }
}
?>

Then try this

}else {

         $message = "Credenciales incorrectas";
         echo "<script type='text/javascript'>alert('$message');</script>";
         header('Refresh: 0; URL=index.php');


      }

Try this one. Probably you have an extra bracket } or is wrongly positioned and you are echoing $message as a string which is wrong. Use concatenation in this way to print your message

$message = "Credenciales incorrectas";
     echo "<script type='text/javascript'>alert('" . $message . "');</script>";
     header('Refresh: 0; URL=index.php');

Like mentioned in the comments you have one curly braket that messes up your code. The else statement is now checking for the first if statement in which you check the request type. But the else case with the count should be the else case to your if statement that checks the number of rows ($count).

Furthermore you need to concatenate strings in your echo because your JS code doesnt know about variables or values from your PHP script like this:

echo "<script type='text/javascript'>alert('" . $message . "');</script>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM