0

I'd like to execute an if-loop where the values within the two fields are compared to the table bestilling within the connected database and then, if there are values within the database that are the same as the ones within the field, will the submit button at the bottom be enabled. However I'm not sure how to get the if-loop to start once the two fields are filled out, help?

<li>Hva er deres Kundenummer?
<input type="text" name="KundeID"> </li>
<li>Hva er deres Passord?
<input type="text" name="password"> </li><br>
<?php
$tjener = "localhost";
$brukernavn = "root";
$passord = "";
$database = "gyldentag takeaway";

$kobling = new mysqli($tjener, $brukernavn, $passord, $database);

function login ($KundeID, $password) {
$result = mysqli_query($con, "SELECT * FROM kunder WHERE KundeID='$KundeID' AND password='$password'");
while($row = mysqli_fetch_array($result)) {
    $success = true;
}
if($success == true) {
    echo 'document.getElementById("Button").disabled = false;';
} else {
    echo '<div class="alert alert-danger">Brukernavn eller Passord er ukorrekt venligst prøv igjen</div>';
}
}
?>
  • **Never** store plain text passwords. You should use [`password_hash()`](http://us3.php.net/manual/en/function.password-hash.php) and [`password_verify()`](http://us3.php.net/manual/en/function.password-verify.php) instead. If you're using a version of PHP prior to 5.5, do **not** use MD5 or SHA1 to hash passwords. Instead you can use [this compatibility pack](https://github.com/ircmaxell/password_compat). – Alex Howansky Apr 28 '17 at 19:59
  • Your code is likely vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 28 '17 at 19:59
  • If it's a front end thing as it sounds you'd need ajax to run the queries and such behind the scenes – clearshot66 Apr 28 '17 at 20:01

1 Answers1

0

You can't do that like that. It's not possible.

PHP code is processed even before the page is sent to your browser. That means once you have a page visible in front of you, PHP has already shut down.

If you need your page to talk to PHP code on the server, you'll have to look at AJAX requests. With those, you can send requests to the server and respond the the result. That way, you can absolutely do what you want.

You'll need to send the input to the server every time it is changed (onchange) and let the server give you a boolean result. Once you have received a true result, you're done ;)

Imanuel
  • 3,596
  • 4
  • 24
  • 46