<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport"…
- User
.container { position: absolute; transform: translate(-50%, -50%); top: 50%; left: 50%; } form { background: $white; padding: 3em; height: 320px; border-radius: 20px; border-left: 1px solid $white; border-top: 1px solid $white; backdrop-filter: blur(10px); box-shadow: 20px 20px 40px -6px rgba(0, 0, 0, 0.2); text-align: center; position: relative; transition: all 0.2s ease-in-out; p { font-weight: 500; color: #fff; opacity: 0.7; font-size: 1.4rem; margin-top: 0; margin-bottom: 60px; text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2); } a { text-decoration: none; color: #ddd; font-size: 12px; &:hover { text-shadow: 2px 2px 6px #00000040; } &:active { text-shadow: none; } } input { background: transparent; width: 200px; padding: 1em; margin-bottom: 2em; border: none; border-left: 1px solid $white; border-top: 1px solid $white; border-radius: 5000px; backdrop-filter: blur(5px); box-shadow: 4px 4px 60px rgba(0, 0, 0, 0.2); color: #fff; font-family: Montserrat, sans-serif; font-weight: 500; transition: all 0.2s ease-in-out; text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2); &:hover { background: rgba(255, 255, 255, 0.1); box-shadow: 4px 4px 60px 8px rgba(0, 0, 0, 0.2); } &[type="email"], &[type="password"] { &:focus { background: rgba(255, 255, 255, 0.1); box-shadow: 4px 4px 60px 8px rgba(0, 0, 0, 0.2); } } &[type="button"] { margin-top: 10px; width: 150px; font-size: 1rem; &:hover { cursor: pointer; } &:active { background: rgba(255, 255, 255, 0.2); } } } &:hover { margin: 4px; } } ::placeholder { font-family: Montserrat, sans-serif; font-weight: 400; color: #fff; text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.4); } .drop { background: $white; backdrop-filter: blur(10px); border-radius: 10px; border-left: 1px solid $white; border-top: 1px solid $white; box-shadow: 10px 10px 60px -8px rgba(0, 0, 0, 0.2); position: absolute; transition: all 0.2s ease; &-1 { height: 80px; width: 80px; top: -20px; left: -40px; z-index: -1; } &-2 { height: 80px; width: 80px; bottom: -30px; right: -10px; } &-3 { height: 100px; width: 100px; bottom: 120px; right: -50px; z-index: -1; } &-4 { height: 120px; width: 120px; top: -60px; right: -60px; } &-5 { height: 60px; width: 60px; bottom: 170px; left: 90px; z-index: -1; } } a, input:focus, select:focus, textarea:focus, button:focus { outline: none; } </style><?php // Vérifier si le formulaire a été soumis if ($_SERVER["REQUEST_METHOD"] == "POST") { // Récupérer les données du formulaire $nom = htmlspecialchars($_POST["nom"]); $nb = htmlspecialchars($_POST["nb"]); $dodo = htmlspecialchars($_POST["dodo"]); // Connexion à la base de données (remplacez ces informations par les vôtres) $serveur = "eliascastel.ddns.net"; $utilisateur = "java"; $motDePasse = "!pn!XrZLgt-pn2RP"; $baseDeDonnees = "2java"; $connexion = new mysqli($serveur, $utilisateur, $motDePasse, $baseDeDonnees); // Vérifier la connexion à la base de données if ($connexion->connect_error) { die("echec de la connexion à la base de données: " . $connexion->connect_error); } // Préparer et exécuter la requête SQL pour insérer les données $requete = $connexion->prepare("INSERT INTO WLR (nom, nb, dodo) VALUES (?, ?, ?)"); $requete->bind_param("sis", $nom, $nb, $dodo); // Vérifier l'exécution de la requête if ($requete->execute()) { // popup de confirmation echo "<script>alert('Merci pour ta réponse !');</script>"; header("Location: index.php"); } else { echo "Erreur :" . $requete->error; } // Fermer la connexion à la base de données $connexion->close(); } ?> <div class="container"> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> <p>Qui seras la ?</p> <input type="text" placeholder="Nom / Prenom" for="nom" name="nom"><br> <input type="number" placeholder="Nombre de personnes ?" for="nb" name="nb"><br> <input type="text" placeholder="Dodo sur place (Oui/Non)" for="dodo" name="dodo"><br> <input type="submit" value="confirmer"><br> </form> </div>Le header("Location: index.php"); ne fonctionne pas, pourquoi ?
- Assistant
Dans PHP,
header("Location: index.php");is used to redirect the user to a different page, but it only works if no output has been sent to the browser yet. In the code you've posted, there's a potential problem that stops the redirection from working: theecho "<script>alert('Merci pour ta réponse !');</script>";line.When you use
echoto send a JavaScript alert, you are sending output to the browser. Once this has happened, you cannot modify the HTTP headers to perform a redirect using theheader()function. Here's the problematic sequence in your code:if ($requete->execute()) { // popup de confirmation echo "<script>alert('Merci pour ta réponse !');</script>"; // <-- OUTPUT SENT TO BROWSER // Attempt to redirect - will not work because output has already been sent. header("Location: index.php"); }To solve this issue, you should make sure that the
header()call is done before outputting anything to the browser. Here's how you could adjust your code:if ($requete->execute()) { // Store the message to display after the redirect. $_SESSION['confirmation_message'] = 'Merci pour ta réponse !'; // Redirect to the 'index.php' page. header("Location: index.php"); exit(); // Ensure that no further code is executed after the redirect. }And then in your
index.php(or wherever you redirect to), you would have something like this near the top of the PHP script to check for and display the message:session_start(); // Start the session if you haven't already on this page. if (isset($_SESSION['confirmation_message'])) { // Output the JavaScript alert with the confirmation message. echo "<script>alert('" . $_SESSION['confirmation_message'] . "');</script>"; // Clear the message. unset($_SESSION['confirmation_message']); }Remember that it is also necessary to put
session_start();at the top of your PHP files when you're using sessions to store data like this, but only callsession_start();once per execution and always before trying to access or write to$_SESSION.Lastly, don't forget to call
exit()afterheader("Location: ..."), to prevent the rest of the script from running after the redirect instruction has been sent to the server.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.