在php中使用javascript警报

在php中使用javascript警报,php,javascript,Php,Javascript,我在一个网站上工作,我从数据库中获取记录以与用户输入匹配,如果数据与数据库中的详细信息不匹配,则必须显示一个javascript警报框。但问题是javascript框也显示在body load上。以下是我的代码: //Storing the value of form's username and password into PHP variables $username = $_POST['username']; $password = $_POST['password']; //This q

我在一个网站上工作,我从数据库中获取记录以与用户输入匹配,如果数据与数据库中的详细信息不匹配,则必须显示一个javascript警报框。但问题是javascript框也显示在body load上。以下是我的代码:

//Storing the value of form's username and password into PHP variables
$username = $_POST['username'];
$password = $_POST['password'];
//This query is to check whether ther user has provided valid details
$query = mysql_query("Select * from accounts where username='$username' and     password='$password'");
$status = mysql_num_rows($query);
/**Now checking whether the number of rows returned by database are greater than 0 to  verify the login
if number of rows are greater than zero we will redirect the user to index page with a     session variable**/

if($status >0)
{
$_SESSION['username'] = $username;
?>
<meta http-equiv="refresh" content="0;url=index.php">
<?}
else
{?>
<script language="javascript">
alert('UserName or Password does not match. Please try again');
</script>
<?}?>

您需要为此设置登录标志

将以下内容添加到您的表单中:

<input type="hidden" name="login_process" value="true" />
替换

else


如果您不希望在用户不尝试登录时显示警报

警告:您的代码极易受到sql注入攻击。同样,你也不应该在数据库中存储用户密码。@Jessica Browne为了完成Daniel所说的,我建议你寻找密码散列,不要只散列密码,如果密码是常量,则始终包括salt和用户名等其他部分。Jessica,这是在哪一页?你能出示表格吗?你能在这个片段之前展示更多的代码吗?我想她正在努力学习在HTML页面中使用PHP。一步一个脚印。在她学会注射之前,不要把她和注射混为一谈。另一方面,你正在使用。为什么这能解决问题?
you miss <?php and Varible
    this Code Use then Check
       <?php 

            $username = $_POST['username'];
            $password = $_POST['password'];

            //This query is to check whether ther user has provided valid details
            $query = mysql_query("Select * from accounts where username='".$username."' and     password='**".$password."**'");
            $status = mysql_num_rows($query);

            /**Now checking whether the number of rows returned by database are greater than 0 to  verify the login if number of rows are greater than zero we will redirect the user to index page with a session variable**/

            if($status >0)
            {
               $_SESSION['username'] = $username;
            ?>
            <meta http-equiv="refresh" content="0;url=index.php">
            **<?php** }
            else
            {?>
            <script language="javascript">
            alert('UserName or Password does not match. Please try again');
            </script>
            **<?php** }?>
else if (isset($_POST['username'])) {
you miss <?php and Varible
    this Code Use then Check
       <?php 

            $username = $_POST['username'];
            $password = $_POST['password'];

            //This query is to check whether ther user has provided valid details
            $query = mysql_query("Select * from accounts where username='".$username."' and     password='**".$password."**'");
            $status = mysql_num_rows($query);

            /**Now checking whether the number of rows returned by database are greater than 0 to  verify the login if number of rows are greater than zero we will redirect the user to index page with a session variable**/

            if($status >0)
            {
               $_SESSION['username'] = $username;
            ?>
            <meta http-equiv="refresh" content="0;url=index.php">
            **<?php** }
            else
            {?>
            <script language="javascript">
            alert('UserName or Password does not match. Please try again');
            </script>
            **<?php** }?>