Je souhaiterais savoir comment utiliser une variable dans plusieurs fonctions.
Je m'explique je recupere un nom et une adresse mail grasse à un formulaire -> ces information ce trouve enregistrer dans la BDD. (fonction insertform)
ensuite je demande une ville parmi une liste de 5 noms je veut que cette information sois stocker sur la même ligne que le nom. (fonction yourchoicecity) Seulement vue que je ne suis sortie de la première fonction je n'ai plus cette donnée $nom.
Comment pourrais je faire.
index.phpformulaire.php
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="styles.css"> <title>Document</title> </head> <body> <?php require('class-bdd.php'); require('getdata.php'); $gestion = new gestion();/* apel à la fonction gestion de class-bdd*/ $gestions = $gestion->creatdb(); $data = $gestion->insert(); /*--------------------------------A deplacer dans les fonction--------------------*/ $servername = 'localhost'; $username = 'root'; $password = ''; $dbase = 'mabase'; $dsn = "mysql:host=$servername;dbname=$dbase"; $sql = "SELECT nom FROM hotel"; try{ $pdo = new PDO($dsn, $username, $password); $lishotel = $pdo->query($sql); if($lishotel === false){ die("Erreur"); } }catch (PDOException $e){ echo $e->getMessage(); } /*-----------------------------------------------------------------------------*/ ?> <header> <img src="img/logo.jpg" alt="logo"> <h1>booking</h1> <nav> <ul class="open-btn"> <!-- Formulaire de creation de compte --> <li> <div > <button class="open-button" onclick="openForm()">s'incrire</button> </div> <?php require("formulaire.php");?> </li> <li> <div class="open-btn"> <button class="open-button" onclick="openForm()">se connecter</button> </div> </li> </ul> </nav> </header> <main> <section class="top"> <h1>Hotel du sud</h1> <h2>indiquez vos dates et votre choix parmi nos hebergements</h2> </section> <section class="global"> <div class="where"> <!-- Fomrulaire choix de la déstination --> <button class="destination">Où allez-vous?</button> <ul id="openlist"> <?php while($row = $lishotel->fetch(PDO::FETCH_ASSOC)) : $liste= $row['nom']?> <li> <form action="<?php yourchoicecity(); ?>" method="post"> <input type="submit" name="choicecity" value=<?php echo $liste?> > </form> </li> <?php endwhile ?> </ul> </div> <div class="date"> <!-- fomulaire de séléction des dates de debut et fin--> <form action="<?php yourchoicedate(); ?>" method="post"> <input class="destination" type="date" name="datedebut" > <input class="destination" type="date" name="datefin"> <input class="destination search" type="submit" name="choicedate" value="Rechercher"> </div> </form> </section> </main> </body> </html>
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 <div class="login-popup"> <div class="form-popup" id="popupForm"> <form action="<?php insertform()?>" method="post" class="form-container"> <h2 class="marginmail">Veuillez vous enregistrer</h2> <label for="mail"> <strong>E-mail</strong> </label> <input type="mail" id="mail" placeholder="Votre Mail" name="mail" required> <label for="nom"> <strong>Nom</strong> </label> <input type="text" id="nom" placeholder="Votre nom" name="nom" required> <button type="submit" name="insert" class="btn">ENREGISTRER</button> <!-- Transfert des infos client dans GETDATA.PHP --> <button type="button" class="btn cancel" onclick="closeForm()">Fermer</button> </form> </div> </div> <script> function openForm() { document.getElementById("popupForm").style.display="block"; } function closeForm() { document.getElementById("popupForm").style.display="none"; } </script>
getdata.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 <?php function insertform(){ if(isset($_POST["nom"])){ $host = 'localhost'; $dbname = 'mabase'; $username = 'root'; $password = ''; $nom = $_POST['nom']; $mail = $_POST['mail']; $pdo = new PDO("mysql:host=$host;dbname=$dbname","$username","$password"); $sql = "INSERT INTO `reservation` (`nom`, `mail`) VALUES ('$nom', '$mail')"; $resultat = $pdo->exec($sql); if(!$resultat) { echo "Échec de l'opération d'insertion"; } else{ return($nom); } } } function yourchoicecity(){ $servername = 'localhost'; $username = 'root'; $password = ''; $dbase = 'mabase'; if(isset($_POST["choicecity"])){ try { $pdo = new PDO("mysql:host=$servername;dbname=$dbase","$username","$password"); $choicecity = $_POST["choicecity"]; $today = date("Y-m-d"); $city = "UPDATE reservation SET choicecity='$choicecity' WHERE nom='$nom'"; $resultcity = $pdo->exec($city); if($resultcity != false){ ?> <script> document.location.replace("index.php" ); </script> <!-- retour à la page d'accueil--> <?php } } catch (PDOException $exc) { echo $exc->getMessage(); exit; } } } function yourchoicedate() { $servername = 'localhost'; $username = 'root'; $password = ''; $dbase = 'mabase'; if(isset($_POST["choicedate"])){ try { // se connecter à mysql $pdo = new PDO("mysql:host=$servername;dbname=$dbase","$username","$password"); // récupérer les valeurs $datedebut = $_POST["datedebut"]; $datefin =$_POST['datefin']; $today = date("Y-m-d"); $date = "UPDATE reservation SET creation='$today', debut='$datedebut', fin='$datefin' WHERE nom= '$nom'"; $resultdate = $pdo->exec($date); if($resultdate != false) { ?> <script> document.location.replace("index.php" ); </script> <!-- retour à la page d'accueil--> <?php } } catch (PDOException $exc) { echo $exc->getMessage(); exit; } } } ?>
Partager