表格不会重定向到响应页面

时间:2014-11-18 20:05:18

标签: php validation redirect

创建了一个表单,它使用php检查错误,但在成功提交后,它不会重定向到我创建的响应页面(thanks.php)。它重定向到具有相同标题和页面格式的同一联系页面,但没有实际文本和bodytext div不显示。我只需要它重定向到我创建的响应页面。有什么帮助吗?

以下是代码:

<?php
 $firstnameerror=$browsererror=$sexerror=$dateerror=$visiterror=$judgementerror=$emailerror=$emailconfirmerror="";

 if (isset($_GET['submit'])){
        $browser = htmlspecialchars($_GET['browser']);
        $firstname = htmlspecialchars($_GET['firstname']);
        $lastname = htmlspecialchars($_GET['lastname']);
        $sex = htmlspecialchars($_GET['sex']);
        $date = htmlspecialchars($_GET['date']);
        $visit = htmlspecialchars($_GET['visit']);
        $judgement = htmlspecialchars($_GET['judgement']);
        $message = htmlspecialchars($_GET['message']);
        $email = htmlspecialchars($_GET['email']);
        $emailconfirm = htmlspecialchars($_GET['emailconfirm']);

        echo "<p>form submitted</p>";

        if (isset($browser) && isset($firstname) && isset($lastname) && isset($sex) && isset($date) && isset($visit)
          && isset($judgement) && isset($message) && isset($email) && isset($emailconfirm)){
            echo "<p>all fields were submitted</p>";}

        if (empty($firstname)){
            $firstnameerror .= "<div class= 'error'>First name is missing</div>";}

        if (empty($browser)){
            $browsererror .= "<div class= 'error'>Tell us where you're from</div>";}

        if (empty($sex)){
            $sexerror .= "<div class= 'error'>Gender is missing</div>";}

        if (empty($date)){
            $dateerror .= "<div class= 'error'>Date is missing</div>";}

        if (empty($visit)){
            $visiterror .= "<div class= 'error'>Tell us what you visited</div>";}

        if (empty($judgement)){
            $judgementerror .= "<div class= 'error'>Tell us what you thought</div>";}

        if (!empty($email) && empty($emailconfirm)){
            $emailerror .= "<div class= 'error'>Please confirm your email</div>";}

        if ($email != $emailconfirm){
            $emailconfirmerror .= "<div class='error'>Make sure your emails match.</div>";}

      if (empty($browsererror) && empty($firstnameerror) && empty($sexerror) && empty($dateerror) && empty($visiterror) && empty($judgementerror) && empty($emailerror)
          && empty($emailconfirmerror)){
          header('Location: thanks.php');
          exit();
        }
      }
?>

 <div class="bodytext">
  <form method="get" action="contact.php">
    <h2>Feedback</h2>
    <fieldset>
    <div>
    <label for="browser">Who are you?</label> <span style="font-size:10px;color:red">*Required</span><br>
    <input list="browsers" name="browser" placeholder="Click Arrow ->">
    <datalist id="browsers">
      <option value="Visitor">
        <option value="Town Resident">
          <option value="Cornell University Student">
            <option value="Ithaca College Student">
              <option value="Cornell University Professor">
                <option value="Ithaca College Professor">
                </datalist><?php echo $browsererror ?>
                </div><br><br>
                <div>
                <label for="firstname">First name</label> <span style="font-size:10px;color:red">*Required</span><br>
                <input type="text" name="firstname" placeholder="First Name" value="<?php echo $firstname ?>"/><br>
                <?php echo $firstnameerror ?>
              </div>
              <div>
                <label for="lastname">Last name</label><br><input type="text" name="lastname" placeholder="Last Name" value="<?php echo $lastname ?>"/>
              </div><br><br>
              <div>
                <label for="gender">Gender:</label>
                <input type="radio" name="sex" value="Male" checked>Male
                <input type="radio" name="sex" value="Female">Female<br>
                <?php echo $sexerror ?>
              </div><br>
              <div>
                <label for="date">Date of Visit</label> <span style="font-size:10px;color:red">*Required</span><br>
                <input type="date" name="date" min="2014-10-03" max="2014-10-05"><br>
                <?php echo $dateerror ?>
              </div><br>
              <div>
                <label for="visit">What did you visit?</label> <span style="font-size:10px;color:red">*Required</span><br>
                <input type="checkbox" name="visit" value="Fairway Market" checked>Fairway Market<br>
                <input type="checkbox" name="visit" value="Car Show">Car Show<br>
                <input type="checkbox" name="visit" value="First Peoples' Festival">First Peoples' Festival<br>
                <input type="checkbox" name="visit" value="Concerts">Concerts<br>
                <input type="checkbox" name="visit" value="Finger Lakes Cider Week">Finger Lakes Cider Week<br>
                <input type="checkbox" name="visit" value="Apple Pie Bake-off">Apple Pie Bake-off<br>
                <input type="checkbox" name="visit" value="Gallery Night">Gallery Night<br>
                <input type="checkbox" name="visit" value="Other">Other<br>
                <?php echo $visiterror ?>
              </div><br>
              <div>
                <label for="judgement">What did you think?</label> <span style="font-size:10px;color:red">*Required</span><br>
                <input type="radio" name="judgement" value="Loved It" checked>Loved It<br>
                <input type="radio" name="judgement" value="Liked It">Liked It<br>
                <input type="radio" name="judgement" value="Neutral">No Opinion/Neutral<br>
                <input type="radio" name="judgement" value="Didn't Like It">Didn't Like It<br>
                <input type="radio" name="judgement" value="Hated It">Hated It<br>
                <?php echo $judgementerror ?>
              </div><br>
              <div>
                <label for="message">Suggestions/Improvements</label><br>
                <textarea name="message" id="message" placeholder="Type your message here" value="<?php echo $message ?>"/></textarea><br><br>
               </div>
               <div>
                Do you want to be contacted about<br>future Downtown Ithaca Events?<br>
                <label for="email">E-mail:</label>
                <input type="email" name="email" placeholder="Type E-mail" value="<?php echo $email ?>"/><br>
                <label for="emailconfirm">Confirm:</label>
                <input type="email" name="emailconfirm" id="emailconfirm" placeholder="Confirm E-mail" value="<?php echo $emailconfirm ?>"/><br><br>
                <?php echo $emailerror ?><?php echo $emailconfirmerror ?></div>
                <input name="ResponseForm" type="hidden" value="thanks.php"/>
                <button type="submit" name="submit">Submit</button>
                </fieldset>
              </form>
          </div>
          <script src="JS/myscript.js"></script>
        </body>
        </html>

3 个答案:

答案 0 :(得分:1)

在回显内容后,您无法重定向。删除此回显,它将重定向:

echo "<p>form submitted</p>";

答案 1 :(得分:0)

empty仅在变量不存在或设置为false时触发。如果创建没有值的变量,empty将不会触发。

因此,请更改条件语句:

empty($browsererror)

致:

!$browsererror

答案 2 :(得分:-1)

不能在任何输出之后使用header("location: somewhere.php");,无论是空格还是H或Hi,都无法使用它。

您必须在显示任何数据之前处理表单数据。此外,您应该使用JS验证,因为这样您就不必向服务器发出innecesarias请求并帮助服务器接收足够的数据并记住,不输出在致电header("location: somewhere.php");

之前的任何事情