Browse Source

first php

l4legenda 3 years ago
commit
d1e0330787
3 changed files with 50 additions and 0 deletions
  1. 25 0
      bd.php
  2. 11 0
      index.php
  3. 14 0
      script.js

+ 25 - 0
bd.php

@@ -0,0 +1,25 @@
+<?php
+
+$db_host = "127.0.0.1"; // Указывайте IP адрес вместо localhost (При указании localhost он ищет ip адрес)
+$db_name = "chat2";
+$db_user = "root";
+$db_password = "";
+ 
+// пример соединения с MySQL при помощи PDO
+$db = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password);
+
+if( isset($_GET["message"]) ){
+	$insert = $db->prepare("INSERT INTO chat (id, message) VALUES (:param1, :param2)");
+	$insert->execute([
+		'param1' => NULL,
+		'param2' => $_GET["message"]
+	]);
+
+}
+
+$user = $db->prepare("SELECT * FROM chat");
+$user->execute();
+
+$user = $user->fetchAll();
+
+print_r(json_encode($user, JSON_FORCE_OBJECT));

+ 11 - 0
index.php

@@ -0,0 +1,11 @@
+<!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>
+    <script src="script.js"></script>
+</body>
+</html>

+ 14 - 0
script.js

@@ -0,0 +1,14 @@
+async function send(message){
+
+
+    let response = await fetch("/bd.php?message=" + message);
+
+    if (response.ok) { // если HTTP-статус в диапазоне 200-299
+      // получаем тело ответа (см. про этот метод ниже)
+      let json = await response.json();
+      console.log( json);
+    }
+
+
+}
+