Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/ajax/extra1/
createtable.php
delete.php
extra1.php
select.php
service.php
signIn.php
signIn.php
80 lines UTF-8 Windows (CRLF)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
/** @var PDO $dbconn */
include "../../../dbconnection.php";
if (isset($_POST["username"]) && isset($_POST["password"])) {
try {
$sql = "SELECT * FROM ajaxextra1 WHERE username=?";
$stmt = $dbconn->prepare($sql);
$data = array($_POST["username"]);
$stmt->execute($data);
if (!$stmt->fetch(PDO::FETCH_ASSOC)) {
$sql = "INSERT INTO ajaxextra1 (username, password)
VALUES (?, ?)";
$stmt = $dbconn->prepare($sql);
$username = htmlspecialchars($_POST["username"]);
$password = htmlspecialchars($_POST["password"]);
$data = array($username, $password);
$stmt->execute($data);
header("location: select.php");
} else {
echo "Användarnamnet är upptaget, försök igen";
}
}
catch (PDOException $e) {
echo $e;
}
}
?>
<form method="post">
<input type="text" name="username" placeholder="Namn" onkeyup="isUnique(this.value)">
<span id="names"></span><br>
<input type="password" name="password" placeholder="Lösenord"> <br>
<input type="submit" id="submit" value="Skicka">
</form>
<script>
async function isUnique(txt) {
const names = document.getElementById("names");
const submit = document.getElementById("submit");
if (txt.length < 3) {
names.innerHTML = "";
} else {
let object = await fetch("service.php?name=" + txt);
let text = await object.text();
names.innerHTML = text;
if (names.innerHTML == "Ok!") {
names.style.backgroundColor = "limegreen";
submit.disabled = "";
} else {
names.style.backgroundColor = "red";
submit.disabled = "disabled";
}
}
}
</script>
</body>
</html>