简体   繁体   English

当对 PHP 脚本使用 GET 请求时,回显的结果不是正确的变量,而是输出页面的 HTML

[英]When using a GET request to a PHP script the echoed result is not the right variable, its outputting the HTML of the page

I am trying to check an email as a duplicate using a GET request to a PHP script.我正在尝试使用对 PHP 脚本的 GET 请求检查电子邮件是否重复。 I have already gotten it to work for usernames on a different page.我已经让它适用于不同页面上的用户名。 The sign-up page sends a request via the email type input box on blur.注册页面通过模糊上的电子邮件类型输入框发送请求。

The HTML的HTML

<div id="email-container">

     <label id="email-not-taken" for="email">Email</label>
     <img id="green-check" src="../images/green-check.png">
     <label id="email-taken">Email Taken</label>
     <input id="email-dc" type="email" name="email" value=" ">

</div>

So the result is a combination of that HTML showing if the email is already taken or not.因此,结果是显示电子邮件是否已被接收的 HTML 组合。

At the bottom, it calls the signup.js script在底部,它调用了 signup.js 脚本

<script src="../js/signup.js"></script>

And here is signup.js这是signup.js

$('document').ready(function() {

    $('#email-dc').on('blur', function() {

        console.log("Inside blur"); // I can get here

        var email = $('#email-dc').val();
        if (email == '') {

            email_state = false;
            console.log("Email Empty"); // I can get here
            return;

        }

        var link = $('#email-dc').val();

        if (link == '') {
    
            // Do nothing
            
        } else {
    
            $.get("process.php?link="+link, function(data) {

                console.log(data); // Right here it spits out the HTML of the page not $result

                if (data == "Taken") {

                    $("#email-dc").css("border","1px solid tomato");
                    $('#email-not-taken').css('display', 'none');
                    $('#email-taken').css('display', 'block');
                    $('#green-check').css('display', 'none');

                } else if (data == "Not Taken") {

                    $("#email-dc").css("border","1px solid #008000");
                    $('#email-not-taken').css('display', 'block');
                    $('#email-not-taken').css('color', '#008000');
                    $('#green-check').css('display', 'block');
                    $('#email-taken').css('display', 'none');

                } else {

                    // Do Nothing

                }
    
            });
    
        }

    });
});

When it gets here当它到达这里

console.log(data);

It spits out the HTML of the page and not the fetched $result它吐出页面的 HTML 而不是获取的 $result

The link gets sent to process.php, which is this链接被发送到 process.php,就是这样

<?php

    SESSION_START();

    if (isset($_SESSION['email'])) {

        $email = $_SESSION['email'];
        $orig = $_SESSION['username'];

    } else {

        header("Location:login.php");

    }

    // Connect to the Database

    $dbc = mysqli_connect('localhost','root','','acoc_main_db') or die("Bad connection");

    $result = '';

    if (isset($_GET['link'])) {

        $link = $_GET['link'];

        if (filter_var($link, FILTER_VALIDATE_EMAIL)) {

            $sql = "SELECT * FROM users WHERE email='$link'";
            $done = mysqli_query($dbc, $sql) or die("Bad sql: $sql");
            
            if (mysqli_num_rows($done) > 0) {
    
                $result = 'Taken';
    
            } else {
    
                $result = 'Not Taken';
    
            }

        } else {

            $sql = "SELECT * FROM users WHERE username='$link'";
            $done = mysqli_query($dbc, $sql) or die("Bad sql: $sql");
            
            if (mysqli_num_rows($done) > 0 && $link != $orig) {
    
                $result = 'Taken';
    
            } else {
    
                $result = 'Not Taken';
    
            }

        }

    }

    echo $result;

?>

The if checks if it's an email and then runs the SQL commands to check for a duplicate and the else statement goes on if it's not an email, meaning it's a username and returns the $result (which the 'else check' on the username is working!). if 检查它是否是一封电子邮件,然后运行 ​​SQL 命令来检查是否有重复,如果不是电子邮件,则 else 语句继续执行,这意味着它是一个用户名并返回 $result(用户名上的“else check”是在职的!)。

So I think it is broken somewhere here所以我认为它在这里的某个地方坏了

        if (filter_var($link, FILTER_VALIDATE_EMAIL)) {

            $sql = "SELECT * FROM users WHERE email='$link'";
            $done = mysqli_query($dbc, $sql) or die("Bad sql: $sql");
            
            if (mysqli_num_rows($done) > 0) {
    
                $result = 'Taken';
    
            } else {
    
                $result = 'Not Taken';
    
            }

        } else {

The link in the email check is not assigning a 'Taken' or 'Not Taken' instead when I console log the resulting data it spits out the HTML of the page, not whether it found something in the SQL commands.电子邮件检查中的链接没有分配“已使用”或“未使用”,而是当我控制台记录结果数据时,它会吐出页面的 HTML,而不是它是否在 SQL 命令中找到了某些内容。

在此处输入图像描述

Thanks for any input!感谢您的任何意见!

So the issue was figured out in the comments above.所以在上面的评论中发现了这个问题。 I checked the network tab and the login.php page was being loaded.我检查了网络选项卡,并且正在加载 login.php 页面。

I thought my signup.php page was sending a $_SESSION['email'] to process.php but it was not, which I feel a little dumb about since if they are signing up those session variables won't be set yet.我以为我的 signup.php 页面正在向 process.php 发送 $_SESSION['email'] ,但事实并非如此,我觉得这有点愚蠢,因为如果他们正在注册,这些会话变量还不会设置。 It was working for user-backend.php since that did have an email session variable set.它适用于 user-backend.php,因为它确实设置了电子邮件会话变量。

It started working when I commented out the code below当我注释掉下面的代码时它开始工作

    SESSION_START();

    if (isset($_SESSION['email'])) {

        $email = $_SESSION['email'];
        $orig = $_SESSION['username'];

    } else {

        // header("Location:login.php");

    }

Then I added this to signup.php然后我将此添加到 signup.php

    SESSION_START();

    $_SESSION['signup'] = true;

I then modified process.php with this new if statement然后我用这个新的 if 语句修改了 process.php

    SESSION_START();

    if (isset($_SESSION['email']) || isset($_SESSION['signup'])) {

        if (isset($_SESSION['signup']) && !isset($_SESSION['email'])) {

            //do nothing

        } else {

            $email = $_SESSION['email'];
            $orig = $_SESSION['username'];

        }

    } else {

        header("Location:login.php");

    }

They now both work!他们现在都工作了!

I should probably learn the network tab better in dev tools.我可能应该在开发工具中更好地学习网络选项卡。

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

相关问题 单击回显的html代码时,PHP从数据库中获取信息 - PHP get information from database when echoed html code is clicked 如何在php变量中存储html和php代码以回显到html页面 - how to store html and php code inside a php variable to be echoed to a html page 脚本类型=“ text / javascript”变量无法捕获PHP回显的JSON - Script type=“text/javascript” variable not catching PHP echoed JSON 如何获取从JavaScript到PHP变量的回显值? - How to get echoed value from javascript to PHP variable? 使用jQuery将PHP回显的字符串分配给Javascript中的变量 - Using jQuery to assign a string echoed by PHP to a variable in Javascript 在php内回显的html表单内使用javascript函数 - Using javascript function inside html form being echoed inside php jQuery无法在PHP回显的HTML上工作 - Jquery is not working on html echoed by php 为什么microAjax中的警报未输出从php文件回显的文本? - Why is alert in microAjax not outputting text echoed from php file? 如何在同一 HTML 页面上显示 HTML GET 请求数据,使用单独的 PHP 文件来获取它? - How do I display the HTML GET request data on the same HTML page, using a separate PHP file to fetch it? 使用jQuery将PHP函数的结果输出到特定的HTML位置 - Outputting result of PHP function to specific HTML location with jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM