简体   繁体   English

else语句中的Javascript不起作用。 消息未出现

[英]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网站进行基本登录,如果检查使用POST发送的数据是否在MySQL表中,并且如果它正确无误,则该功能将非常简单,它将允许用户继续处理数据不正确的信息,应显示一条消息,提示凭证不正确:

<?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. 到目前为止,if语句一直有效,甚至else语句仍然有效,但该消息根本没有显示出来,我试图查看问题出在哪里,但我找不到它。

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. 可能您有一个多余的括号}或位置错误,并且您在将$message作为字符串错误地回显。 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. else语句现在正在检查用于检查请求类型的第一个if语句。 But the else case with the count should be the else case to your if statement that checks the number of rows ($count). 但是带有计数的else情况应该是if语句的else情况,该语句检查行数($ 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: 此外,您还需要在回显中连接字符串,因为您的JS代码不了解PHP脚本中的变量或值,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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