猿问

验证 PHP 后如何留在页面上

我有一个问题...我创建了一个重定向循环,但我不知道如何修复它或以其他方式执行它。我有一个registration.php,其中有一个表单,它将post-data 发送到validation.php,用于验证并将数据发送到数据库中。到目前为止,一切都很好。但是,如果来自 from 的数据无效,我不能留在registration.php 上并显示错误消息。


所以我看到了一个教程,那个人在他的registration.php中使用了类似的东西。如果验证中出现问题,他会重定向到数据来自的同一页面,在这种情况下,它将从我的 validation.php 回到我的registration.php


header("Location: registration.php?error=".$err_msg);

        exit();


为了获取 $err_msg 的值并将其显示在registration.php 上,他将registration.php 中的值从$_GET['error'] 中取出。如果我这样做,我会得到一个重定向循环。没有循环,他是如何做到的。这是我在registration.php中的表格。


registration.php


<?php 

     if(!empty($_GET['error'])){

        $err_msg= $_GET['error'];

         echo '<p class="error_msg">'.$err_msg. '</p>';  

       }

?>


<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="validation.php">

                    <div class="col-sm-5 col-sm-offset-1">

                            <div class="form-group col-md-14">

                                    <label for="inputState">Geschlecht *</label>

                                    <select id="inputState" name="gender" class="form-control" required="required">

                                        <option selected>Bitte Wählen...</option>

                                        <option value="frau">Frau</option>

                                        <option value="herr">Herr</option>

                                    </select>

                                  </div>

                        <div class="form-group">

                            <label>Vorname *</label>

                            <input type="text" name="firstname" class="form-control" required="required">

                        </div>

我现在处于重定向循环中,并在 chrome 中收到 ERR_TOO_MANY_REDIRECTS 错误。我的错误是什么,他是如何做到这一点而不陷入循环的?


我只想回到registration.php 来显示一个err_msg。


或者有没有办法验证registration.php中的表单数据,并在验证后将它们发送到validation.php以将数据插入数据库。我不想从 registratio.php 中插入它们


侃侃无极
浏览 131回答 2
2回答

守着星空守着你

只需在registration.php中添加以下代码:&nbsp; &nbsp; &nbsp; &nbsp;<?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session_start();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($_SESSION['message'])){ echo $_SESSION['message'];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($_SESSION['message']);}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;?>&nbsp;在您的 validation.php 文件中添加代码:&nbsp; &nbsp; &nbsp; &nbsp;if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['message']= "Ungültige Emailadresse!";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header("Location: registration.php");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; &nbsp; &nbsp; }

郎朗坤

使用以下代码更新您的 validation.php 文件:&nbsp; &nbsp; $err_msg = "";&nbsp; &nbsp; if (!isset($_POST["gender"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST["firstname"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST["lastname"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST["company"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST["street"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST["hnr"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST["plz"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST["city"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST["email"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_POST["password"])) {&nbsp; &nbsp; &nbsp; &nbsp; $err_msg = "Bitte füllen Sie alle Felder aus!";&nbsp; &nbsp; }&nbsp; &nbsp; if ($_POST['gender'] !== "herr" || $_POST['gender'] !== "frau") { //Validierung-&nbsp; &nbsp; &nbsp; &nbsp; $err_msg = "Bitte wählen Sie eine Ansprache!";&nbsp; &nbsp; }&nbsp; &nbsp; elseif(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $err_msg = "Ungültige Emailadresse!";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (preg_match('/[A-Za-z]+/', $_POST['firstname']) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $err_msg = "Ungültiger Vorname!";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (preg_match('/[A-Za-z]+/', $_POST['lastname']) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $err_msg = "Ungültiger Nachname!";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (preg_match('/[A-Za-z0-9]+/', $_POST['company']) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $err_msg = "Ungültiger Firmenname!";&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (preg_match('/[\D][\-]?/', $_POST['street']) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $err_msg = "Ungültige Straße!";&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (preg_match('/[0-9]{1-3}/', $_POST['hnr']) ==&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $err_msg = "Ungültige Straße!";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (preg_match('/[0-9]{5}/', $_POST['plz'])&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;== 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $err_msg = "Ungültige Postleitzahl!";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (preg_match('/{10}/', $_POST['city'])&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $err_msg = "Ungültige Stadt!";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (preg_match('/^(?=.*[a-z])(?=.*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$_POST['password']) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $err_msg = "Passwörter müssen:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Einen Großbuchstaben enthalten&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Einen Kleinbuchstaben enthalten&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Eine Zahl enthalten&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ein Sonderzeichen enthalten&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Und mindestens 8 Zeichen lang&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sein!";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; if($err_msg !== ""){&nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['message']= $err_msg;&nbsp; &nbsp; &nbsp; &nbsp; header("Location: registration.php");&nbsp; &nbsp; }&nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; header("Location: account.php");&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答