当我将php代码放在脚本标记

时间:2018-04-10 07:10:57

标签: javascript php modal-dialog bootstrap-modal

我正在为我的项目构建登录功能。 问题是,在第一次加载页面时,它会显示一个模态,然后用户必须输入用户名和密码才能访问页面(显示模式的同一页面。

模态完美无缺,直到我在脚本标记中放入php代码。你知道我应该改变什么使其有效吗? 这是代码:

<script>
    $(document).ready(function () {
        $('#mymodal').modal({
            backdrop: 'static'
        });
    });
    $(".redirect").click(function ()
    {
<?php
$con = mysqli_connect("localhost", "root", "");
mysqli_select_db($con, "coba3thesis");

if ($_POST['check'] == "send") {
    $username = $_POST['username'];
    $pass = $_POST['pass'];
}

$check = mysqli_query($con, "select * from admin where username='$username'");
$checkrows = mysqli_num_rows($check); //to check username when verifying
while ($row = mysqli_fetch_assoc($check)) {
    $username = $row['username'];
    $pass2 = $row['password'];
}

if ($checkrows = 0) {
    echo "<script>alert('SORRY! You are inserting the wrong username or the username were not existed'); location.href='';</script>";
} else {
    if ($pass !== $pass2) {
        echo "<script>alert('SORRY! You are inserting the wrong password, please insert the correct one!'); location.href='';</script>";
    } else {
        echo "<script>location.href='admin%page.html';</script>";
    }
}
?>
    });
</script>

在此代码中:

if($_POST['check'] == "send"){
     $username = $_POST['username'];
     $pass = $_POST['pass'];
}

我使用此代码以通过添加隐藏输入方法获取插入的数据(传递和用户名)(我在其他功能上使用此方法),隐藏输入的名称为“check”,值为“发送”。

主要问题是,在我输入php代码后,模态不会显示。 谢谢你的帮助。

这是更新的代码:

if($checkrows=0){
                echo "SORRY! You are inserting the wrong username or the username were not existed";
                header('Location: admin%page.php');                 
            }else{
                if($pass !== $pass2){
                    echo "SORRY! You are inserting the wrong password, please insert the correct one!";
                    header('Location: admin%page.php');
                }else{
                    header('Location: admin%page.php'); ;
                }
            }

1 个答案:

答案 0 :(得分:1)

以下是测试PHP的几个步骤

  1. 将这两行添加到您的代码中以获取PHP反馈。

    error_reporting(E_ALL); ini_set('display_errors','1');

  2. 这些将为您启用所有php的错误报告。

    1. 确保您的连接正确无误。它可能会给您的网站带来500错误并导致页面崩溃。

    2. 您不需要在回声中使用脚本标签,因为当用户收到页面时,服务器将评估并运行所有PHP代码并将其取出。