【问题标题】:Navigating to a different html page using php使用 php 导航到不同的 html 页面
【发布时间】:2017-02-03 08:55:40
【问题描述】:

我正在编写 PHP 代码来验证 mysql 数据库中是否存在用户名和密码。当我单击登录页面 (index.html) 上的登录按钮时,将调用此 php。我的php代码如下:

<?php
require_once 'dbconfig.php';

// Unescape the string values in the JSON array
$logindata = stripcslashes($_POST['pLogData']);

// Decode the JSON array
$logindata = json_decode($logindata,TRUE);

// now $tableData can be accessed like a PHP array
$user_email = $logindata['user'];
$user_password = $logindata['pass'];

//First lets get the username and password from the user
$query = $db_conn->prepare("SELECT * FROM tblUsers WHERE email=:user_email LIMIT 1");

$query->execute(array(':user_email'=>$user_email));

$userRow=$query->fetch(PDO::FETCH_ASSOC);

if($query->rowCount() > 0) {
    if(password_verify($upass, $userRow['pass'])) {
        $_SESSION['user_session'] = $userRow['userid'];
        if($userRow['userid'] == 1) {
            header("Location: adminPanel.html");
        } else {
            header("Location: main_dashboard.html");
        }
        // return true;
    } else {
        header("Location: index.html");
        // return false;
    }
}
?>

ajax调用代码为:

function login(username, passwd) {
   var loginData = {
       'user': username,
       'pass': passwd
    };

   loginData = $.toJSON(loginData);

   $.ajax({
       type: "POST",
       url: "loginValidate.php",
       data: "pLogData=" + loginData,
       success: function(msg) {
           console.log(msg);
           // return value stored in msg variable
       }
   });
}

除了在 php 中设置的导航没有发生外,它工作正常。我无法弄清楚错误在哪里。它必须导航到的整个 html 正在打印到控制台中。

任何帮助将不胜感激。

【问题讨论】:

  • 通过 ajax 是行不通的。返回file name 并在javascript 中使用window.location=filename。或提交表单,如果 uncussessfull 重定向到 main_dashboard.html 上的当前 else。
  • @Suchit,您的建议可行,但我需要访问服务器以验证用户名和密码。使用php很容易。如果我用 php 文件替换 html,它可以切换。我想知道为什么它没有切换到 html 文件。
  • .html 无法识别 php 代码。如果您想使用 ajax,则在验证后返回文件名并使用它来重定向。
  • 好的。会试试的
  • 您可以使用 ajax 加载整个 main_dashboard.html,然后使用 innerHTML 将您的 html 内容写入您的 index 页面的正文。

标签: javascript php jquery html mysql


【解决方案1】:

通过 ajax 将无法正常工作。返回文件名并在javascript中使用window.location=filename。或提交表格,如果 uncussessfull 重定向到 main_dashboard.html(the desired page) 上的当前 else。

如果您返回文件名,您可以这样做:

 success: function(filename) {
       window.location=filename;
   }

为此删除标题位置并使用echo filename,如 main_dashboard.html 或其他。

【讨论】:

    【解决方案2】:

    感谢您提出的所有宝贵建议。我设法解决了这个问题。工作代码如下:

    if($query->rowCount() > 0) {
        $upass = $userRow['pass'];
        // if(password_verify($user_password, $userRow['pass'])) {
        if($user_password == $upass) {
            //echo "Check verified";
            $_SESSION['user_session'] = $userRow['userid'];
    
            if($userRow['userid'] == 1) {
                //header("Location: http://localhost:90/codebase-test/adminPanel.html");
                //return $userRow['userid'];
                $filename = "adminPanel.html";
                //return $filename;
            }
            else{
                $filename = "main_dashboard.html";
                //return $filename;
            }
            echo $filename;
           // return true;
        }
        else {
            echo "index.html";
            //echo $filename;
            //return false;
        }
    }
    

    我只发布了我更改的部分。它现在正在工作。

    编辑 我还对 ajax 调用做了一些小改动。代码如下:

    $.ajax({
        type: "POST",
        url: "loginValidate.php",
        data: "pLogData=" + loginData,
        success: function(filename){
            window.location = filename;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 2016-04-06
      相关资源
      最近更新 更多