Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbsrvprg/exercises/ajax/

get_matching_names.php
index.php
ovn_1 copy.php
ovn_1.php

ovn_1.php

85 lines UTF-8 Windows (CRLF)
<?php
ini_set
('display_errors'1);
ini_set('display_startup_errors'1);
error_reporting(E_ALL);
?>
<?php
try {
    include(
"../databaser/dbconnection.php");
    
$sql "";

    
$sql "
        CREATE TABLE IF NOT EXISTS ajax_ovn_1 (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(100) NOT NULL,
        score INT NOT NULL
        );"
;
    
$dbconn->exec($sql);

    
$sql "TRUNCATE TABLE ajax_ovn_1";
    
$dbconn->exec($sql);

    
$people = [
        [
"Harry Molin"100],
        [
"Henry Goransson"20],
        [
"Harry Molajn"1],
        [
"Jakob"12],
        [
"Hanspeter Keller"13],
        [
"Anna"15],
        [
"Niclas"4],
        [
"Tobias"14],
        [
"Jack"15],
        [
"Teo"6]
    ];

    
$sql "INSERT INTO ajax_ovn_1 (name, score) VALUES (?,?)";
    
$stmt $dbconn->prepare($sql);
    foreach (
$people as $person) {
        
$stmt->execute($person);
    }
} catch (
PDOException $e) {
    echo (
$e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="sv">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Övning 1</title>
</head>

<body>
    <form>
        <input type="text" id="name_input" onkeyup="proposeNames()"><label for="name_input">Namn</label><br>
        <div id="proposed_names"></div>
    </form>
    <script>
        function get(id) {
            return (document.getElementById(id));
        }

        let xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                let text = xmlhttp.responseText;
                text = text.replaceAll(",", "<br>");
                get("proposed_names").innerHTML = text;
            }
        }

        function proposeNames() {
            if (get("name_input").value) {
                // Bara om den faktiskt är ifylld.
                const input = get("name_input").value;
                xmlhttp.open("GET", "get_matching_names.php?part=" + input, true);
                xmlhttp.send();
            } else {
                get("proposed_names").innerHTML = "";
            }
        }
    </script>
</body>

</html>