Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/exercises/ajax/ajax/

ajax.php
ajaxClient.php
ajaxService.php
createtable.php
insert.php

insert.php

54 lines UTF-8 Windows (CRLF)
<!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 
  
if (isset($_POST["name"]) && isset($_POST["points"])) {
    try {
      
/** @var PDO $dbconn */
      
include "../../../dbconnection.php";

      
$sql "INSERT INTO ajaxtest (name, points)
      VALUES (?, ?)"
;

      
$stmt $dbconn->prepare($sql);

      
$data = array($_POST["name"], $_POST["points"]);
      
$stmt->execute($data);
    }
    catch (
PDOException $e) {
      echo 
$e;
    }
  }
  
?>

  <form method="post">
    <input type="text" name="name" placeholder="Namn"> <br>
    <input type="number" name="points" placeholder="Poäng"> <br>

    <input type="submit" value="Skicka">
  </form>

  <?php 
    
try {
      
$sql "SELECT * FROM ajaxtest";

      
$stmt $dbconn->prepare($sql);

      
$data = array();
      
$stmt->execute($data);

      while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo 
$res["name"] . "<br>";
      }
    }
    catch (
PDOException $e) {
      echo 
$e;
    }
  
?>
</body>
</html>