0

So i am trying to make an asynchronous call of a PHP script but for some reason there is an error: Maximum call stack size exceeded that is making my site lagging a LOT. There must be a loop somewhere in my code but i can't really find it. I am looking for the past 3 days and still nothing. If someone could spot anything i would be really greatfull. Thanks in advance!

PHP code:

<?php

    require_once 'li.php'; //File with the constants    

    $Username = $_POST['Username'];

    $query = "SELECT Username
                FROM user_info
                WHERE Username = ?
                LIMIT 1";

    if($stmt = $conn->prepare($query)){   //Prepares the statement!

        $stmt->bind_param('s', $Username); 
        $stmt->execute(); //Executes it!

        if($stmt->fetch()){ //Checks if the query returns anything!

            $stmt->close(); //If yes then closes the prepared statement!
            $error = "Username taken";
            echo $error;
        }   
    }
?> 

AJAX/JS code:

$(document).ready(function(){

    $('#Username').on("keyup", function(){

        $.ajax({
            type: 'POST',
            url: 'NameValidation.php', //Your required php page
            data: { Username: Username }, //pass your required data here
            async: true,
        }).done(function(response){

            if(response != ""){
                $('#UsernameLabel').html("Username Taken!");
            }
        });     
    });
});

In case you didn't understand what i want to do with this code let me explain some more. I want every time the Username input changes to search if the username already exists in the database and alert the user by changing the Label text.

PS: Dont worry about SQL injection security i'll add that later when i fix this problem! ^-^

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
GeorgeS
  • 19
  • 6
  • I don't frequently use JQuery but would sending an AJAX call on the `oninput` DOM event (when a username has been fully entered) be more suitable than `keyup` (every key press)? e.g. `.on("input", function(){`... – Benjy1996 Jan 17 '16 at 12:27
  • I'll check that out as soon as possible ^-^ thx – GeorgeS Jan 17 '16 at 12:29
  • I think I got confused with `onchange` (when the input loses focus) XD. Try that if `oninput` doesn't work. – Benjy1996 Jan 17 '16 at 12:38
  • Set `async: false,` in your ajax request. – Rajdeep Paul Jan 17 '16 at 12:45
  • I'll make sure i will ^-^ and let you know! – GeorgeS Jan 17 '16 at 12:45
  • async: false. I tried that before i post this and doesnt work sadly! – GeorgeS Jan 17 '16 at 12:46
  • Are there (too) many AJAX calls logged in your browser's dev console network tab (so a problem with the client side JS)? Does running the PHP script without AJAX (e.g. `domain.com/myscript.php`) cause lag (a server-side problem)? – Benjy1996 Jan 17 '16 at 12:50
  • I guess some kind of loop exists in your code. Check [this answer](http://stackoverflow.com/a/6095695/5517143). – Rajdeep Paul Jan 17 '16 at 12:54
  • I've also just re-looked at your JS - are you sure you are passing the `Username` value in the AJAX call e.g. `data: { Username: Username }`? – Benjy1996 Jan 17 '16 at 12:57
  • Yea i just did that @mestarted pointed that out. It fixed the lag. Thanks a lot everyone <3 Got to love the community! – GeorgeS Jan 17 '16 at 13:06
  • Thanks a lot again <3 – GeorgeS Jan 17 '16 at 13:07

1 Answers1

0

you are not closing connection if query returns nothing, try changing the code to :

if($stmt->fetch()){ //Checks if the query returns anything!
     $error = "Username taken";
     echo $error;
}  
$stmt->close(); //close connection in any case
mestarted
  • 413
  • 4
  • 11