0

Hello fellow developers.

I'm trying to check if an ID exists in the database using JS prompt as a input method and PHP as the database checker. My issue is that I can't pass the inputted ID into PHP and then give a response to the JS if the ID exists or not.

JS Code:

function clockInPrompt() {
    var eID = prompt("Please enter your employee ID");
    if(eID != null) {
        var call_checker = <?php echo check_eID($conn) ?>;
        if(id_check_response) alert("true");
        else alert("false");        
    }           
}

PHP Code:

function check_eID(mysqli $conn) {
    $id = 123;
    $sql = mysqli_query($conn, "SELECT `eID` FROM `employees` WHERE `eID` = '$id'");
    if(mysqli_num_rows($sql) <= 0) echo '<script>var id_check_response = false;</script>';
    else echo '<script>var id_check_response = true;</script>';
}

With the code above I'm at least trying to get a response from the PHP.

johannchopin
  • 13,720
  • 10
  • 55
  • 101
  • 2
    Does this answer your question? [How to put php inside JavaScript?](https://stackoverflow.com/questions/3345457/how-to-put-php-inside-javascript) – Alex Feb 29 '20 at 18:40
  • Unfortunately this doesn't answer my question. The problem with my code is that I'm not only trying to get the variables from PHP to JS and vise-versa I'm as well calling a PHP function from JS and its failing. – Roman Buben Feb 29 '20 at 18:46
  • 1
    if you want some PHP to execute and feed back the result to some Javascript which is running in a page, then you need to use AJAX to make a new request to the server. – ADyson Feb 29 '20 at 22:52
  • Not only are PHP and Javascript two completely separate and independent languages ... but they each execute in completely different places, at different times (on the server, and in the browser, respectively). Your best recourse is what Alex said (effectively, what you're already doing): make your SQL query in PHP, and pass the "result" as Javascript in the returned HTML page. PS: *PLEASE ALWAYS USE PREPARED STATEMENTS*: https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php – FoggyDay Apr 21 '20 at 23:22

0 Answers0