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

Viewed 50

I am trying to check an email as a duplicate using a GET request to a PHP script. 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

<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.

At the bottom, it calls the signup.js script

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

And here is 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

The link gets sent to process.php, which is this

<?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!).

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.

enter image description here

Thanks for any input!

1 Answers

So the issue was figured out in the comments above. I checked the network tab and the login.php page was being loaded.

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. It was working for user-backend.php since that did have an email session variable set.

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

    SESSION_START();

    $_SESSION['signup'] = true;

I then modified process.php with this new if statement

    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.

Related