标题,会话,php验证

时间:2014-11-21 17:12:40

标签: php session header

我的验证表格存在很大问题。基本上我创建了一个表单,一旦提交,将重定向到另一个页面。要做到这一点,我使用标题(“位置:aaaaa.php”),显然它的工作原理。问题是通过这样做,验证不再起作用,事实上如果我没有输入任何数据并按提交,我将被重定向到第二页而不会出现错误。如果我删除标题,验证将再次起作用。 而且我的会话方法有一个很大的问题。当按下按钮提交时,我尝试了使用它将数据传输到第二页的不同方式,但它不起作用,直到现在还没有人能够帮助我。在我要放在下面的代码中,我删除了会话方法,我希望你能帮助我。


                      
                   
                  伦敦飞行社                           

        if ($_SERVER["REQUEST_METHOD"] == "POST") {
           if (empty($_POST["name"])) {
             $nameErr = "Name is required";
           } else {
             $name =($_POST["name"]);
             // check if name only contains letters and whitespace
             if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
               $nameErr = "Only letters and white space allowed"; 
             }
           }

        if (empty($_POST["surname"])) {
             $surnameErr = "Surname is required";
           } else {
             $surname =($_POST["surname"]);
             // check if name only contains letters and whitespace
             if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
               $surnameErr = "Only letters and white space allowed"; 
             }
           }

           if (empty($_POST["email"])) {
             $emailErr = "Email is required";
           } else {
             $email =($_POST["email"]);
             // check if e-mail address is well-formed
             if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
               $emailErr = "Invalid email format"; 
             }
           }

           if (empty($_POST["telephone"])) {
             $telephoneErr = "Number is required";
           } else {
             $telephone =($_POST["telephone"]);
             // check if name only contains letters and whitespace
             if (!preg_match("/^[0-9\_]{7,20}/",$telephone)) {
               $telephoneErr = "Enter correct telephone number"; 
             }
           }

            if (empty($_POST["date"])) {
             $dateErr = "Date is required";
           } else {
             $date =($_POST["date"]);
             // check if name only contains letters and whitespace
             if (!preg_match("~^\\d{1,2}[/.-]\\d{2}[/.-]\\d{4}$~",$date)) {
               $dateErr = "Enter correct date of birth"; 
             }
           }

           if (empty($_POST["luggage"])) {
             $luggageErr = "Choose one of the options";
           } else {
             $luggage =($_POST["luggage"]);
           }





        $weight = $_POST['weight'];
        $height = $_POST['height'];
        if(isset($_POST['submit']))
        {
        $total = ($weight+$height)/10;
        }

        header("Location: thankyou.php");

        }





        ?>

            <h2 id="title">London Flight Agency</h2><!-- This tag is used to define HTML heading (h1 to h6), this particular one defines the most important heading -->             
            <form id="form" method="post" name="myForm" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" onsubmit="return validateForm(this);"><!-- The <form> tag is used to create an HTML form for user input -->
                <h4 class="subtitle"><strong>Personal Details</strong></h4>
                <fieldset>                      
                    Enter here all your details (all of them are compulsory.)

                    <br />
                    <br />

                        <select name="Title" id="title" value="<?php echo $title;?>" onblur="validateSelect(name)">
                            <option value="empty">Title</option>
                            <option value="Mr">Mr</option>
                            <option value="Mrs">Mrs</option>
                            <option value="Miss">Miss</option>
                            <option value="Dr">Dr</option>
                        </select><br /><br />
                    <span class="validateError" id="titleError" style="display: none;">You must select a title!</span>                          
                    <span id="error8"><?php echo $titleErr;?></span>
                    <table  
                        <tr>
                            <td><label for="firstname">First Name:</label></td>
                            <td><input type="text" name="name" value="<?php echo $name;?>"                                       id="name" onblur="validateName(name)"></td>
                            <span id="nameError" style="display: none;"></span>
                            <span id="error1"><?php echo $nameErr;?></span>
                        </tr>

                        <tr>    
                            <td><label for="surname">Surname:</label></td>
                            <td><input type="text" name="surname" value="<?php echo $surname;?>"                                   id="name" onblur="validateSurname(surname)"></td>
                            <span id="surnameError" style="display: none;"></span>
                            <span id="error2"><?php echo $surnameErr;?></span>
                        </tr>

                        <tr>    
                            <td><label for="email">Email address:</label></td>
                            <td><input type="text" name="email" value="<?php echo $email;?>"                                   id="email" onblur="validateEmail(email)"></td>
                            <span id="emailError" style="display: none;"></span>
                            <span id="error3"><?php echo $emailErr;?></span>
                        </tr>

                        <tr>    
                            <td><label for="telephone">Telephone No:</label></td>
                            <td><input type="text" name="telephone" value="<?php echo $telephone;?>" id="telephone" onblur="validateTelephone(telephone)"></td>
                            <span id="telephoneError" style="display: none;"></span>
                            <span id="error4"><?php echo $telephoneErr;?></span>
                        </tr>

                        <tr>    
                            <td><label for="date">Date of birth:</label></td>
                            <td><input type="text" name="date" value="<?php echo $date;?>"                                       id="date" onblur="validateDate(date)"></td>
                            <span id="dateError" style="display: none;"></span>
                            <span id="error5"><?php echo $dateErr;?></span>
                        </tr>
                    </table>

                </fieldset>

                <h4 class="subtitle"><strong>Flight Details</strong></h4>

                <fieldset>
                             <p>Hand luggage:</p><br />
                                 <input type="radio" name="luggage" <?php if (isset($luggage) &&                                      $luggage=="Yes") echo "checked";?>value="Yes" id = "myRadio" required onblur="myFunction()">Yes
                                 <input type="radio" name="luggage" <?php if (isset($luggage) &&                                      $luggage=="No") echo "checked";?>value="No" id = "myRadio" required onblur="myFunction()">No

                            <span id="luggageError" style="display: none;"></span>
                            <span id="error6"><?php echo $luggageErr;?></span>

                                 <br /><br />

                                 <label for="extrabag">Include free extra bag</label>
                                 <input type="checkbox" name="extra" id="check" value="bag">

                                 <br />

                                 <br />

                                    <select name="option" id="card" onblur="validatePayment(card)">
                                        <option value="empty">Payment Card</option>
                                        <option value="Visa">Visa Debit Card</option>
                                        <option value="MasterCard">MasterCard</option>
                                        <option value="PayPal">PayPal</option>
                                        <option value="Maestro">Mestro</option>
                                        <option value="Visa Electron">Visa Electron</option>    
                                    </select><br />
                                <span class="validateError" id="cardError" style="display: none;">You must select your payment card!</span> 


                </fieldset>  

                <h4 class="subtitle"><strong>Luggage Details</strong></h4>

                <fieldset>                      
                        <p>Enter weight and height of your luggage.</p>

                        Your Weight(kg): <input type="text" name="weight" size="7"><br />
                        Your Height(cm): <input type="text" name="height" size="7"><br />
                        <span id="error7"><?php echo $measureErr;?></span>
                        <input type="button" value="Tot. price" onClick="totalPrice()"><br /><br />

                        Total Price: <input type="text" name="total" value="<?php echo $total?>" size="10"><br /><br /> 

                            <input type="reset" value="Reset">
                            <input type="submit" value="Submit" name="submit">                  
                </fieldset>

            </form>
    </body>

它没有显示代码的第一部分,我不知道是哪个原因。我将在下面发布:


                      
                   
                  伦敦飞行社                           

1 个答案:

答案 0 :(得分:0)

问题是您正在检查方法是否发布,然后重定向到新页面。

您必须检查您的条目是否有效,然后重定向。

示例您可以创建一个数组数组,并在出现无效内容时使用array_push推送新数据,然后像这样检查:

 if( count( $array ) == 0 )
     header('Location:YOUR_NEW_PAGE.php');

这是您可以实施的最简单的解决方案之一。

祝你好运