From f8c67fc64b19c4125248877f6d6fd50013ac794d Mon Sep 17 00:00:00 2001
From: Nawreua <erwan.auer@inria.fr>
Date: Tue, 11 Mar 2025 13:36:20 +0100
Subject: [PATCH] =?UTF-8?q?TP5=20:=20ajout=20d'un=20formulaire=20avec=20re?=
 =?UTF-8?q?qu=C3=AAte=20AJAX?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 actions/get.php        | 10 ++++++----
 pages/pageForm.php     | 14 ++++++++++++++
 static/js/ajax.js      | 15 +++++++++++++++
 static/js/blogfolio.js |  0
 4 files changed, 35 insertions(+), 4 deletions(-)
 create mode 100644 static/js/ajax.js
 delete mode 100644 static/js/blogfolio.js

diff --git a/actions/get.php b/actions/get.php
index 4d8a893..ece647f 100644
--- a/actions/get.php
+++ b/actions/get.php
@@ -1,7 +1,9 @@
-<?php require_once(__DIR__.'/../static/php/head.php'); ?>
-<?php echo htmlspecialchars($_GET['name']).' | Score : '; ?>
-<?php $score = $ddb->query('
+<?php
+require_once(__DIR__.'/../config/config.php');
+
+$score = $ddb->query('
   SELECT score FROM users WHERE name = \''.$_GET['name'].'\'
 ')->fetch();
-  echo $score[0]
+
+echo htmlspecialchars($_GET['name']).' | Score : '.$score[0]
 ?>
diff --git a/pages/pageForm.php b/pages/pageForm.php
index 8162e34..ac91f18 100644
--- a/pages/pageForm.php
+++ b/pages/pageForm.php
@@ -1,6 +1,7 @@
 <?php require_once(__DIR__.'/../static/php/head.php'); ?>
 <body>
   <?php require_once(APP_ROOT.'/static/php/navbar.php'); ?>
+  <!-- Formulaires classiques -->
   <form action="/actions/post.php" method="post">
     <label for="name"><?php echo $trad['form_name'][$lang]; ?></label>
     <input name="name" id="name" type="text">
@@ -17,4 +18,17 @@
 
     <button type="submit"><?php echo $trad['search'][$lang]; ?></button>
   </form>
+  <br>
+  <!-- Formulaire AJAX -->
+  <script src="/static/js/ajax.js"></script>
+  <div>
+    <form onsubmit="submitFormGetAjax(this)">
+      <label for="name"><?php echo $trad['form_name'][$lang]; ?></label>
+      <input name="name" id="name" type="text">
+
+      <button type="submit"><?php echo $trad['search'][$lang]; ?></button>
+    </form>
+    <br>
+    <label id="result"></label>
+  </div>
 </body>
diff --git a/static/js/ajax.js b/static/js/ajax.js
new file mode 100644
index 0000000..89cc26a
--- /dev/null
+++ b/static/js/ajax.js
@@ -0,0 +1,15 @@
+function submitFormGetAjax(form) {
+  event.preventDefault();
+
+  var request = new XMLHttpRequest();
+  request.onreadystatechange = function() {
+    // uniquement en cas de requête envoyée et réussie
+    if (this.readyState == 4 && this.status == 200)
+      document.getElementById("result").innerHTML = request.responseText;
+  };
+
+  let name = encodeURIComponent(form.name.value.trim());
+
+  request.open("GET", "/actions/get.php?name=" + name, true);
+  request.send();
+}
diff --git a/static/js/blogfolio.js b/static/js/blogfolio.js
deleted file mode 100644
index e69de29..0000000
-- 
GitLab