【问题标题】:PHP form appears below again after submitting提交后PHP表单再次出现在下方
【发布时间】:2013-10-31 06:34:31
【问题描述】:

我有一个注册表格enroll.php,它提交给自己,如果所有输入都有效,它包括confirm.php,我必须将数据保存到表格中。

看起来像这样 -

注册.php

  <?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    include('validate.php');

    if($valid) {
        require_once ('getDBconnection.php');        
        include ('confirm.php'); 
    }
}
function test_input($data) {
         $data = trim($data);
         $data = stripslashes($data);
         $data = htmlspecialchars($data);
         return $data;
    }
?>
<!DOCTYPE html >
<html>
// all html code with banner and navigation and form elements.

所以当我在确认文本后在整个页面下方获得注册确认时,我会看到我的表单,其中包含所有横幅和导航以及所有 div 内容和表单。

是不是因为我包含了confirm.php?

如果有什么问题请告诉我我刚开始学习php。

【问题讨论】:

    标签: php


    【解决方案1】:

    尝试在indlude confirm.php 之后添加exit 以停止使用菜单等执行整个站点。

    if($valid) {
        require_once ('getDBconnection.php');        
        include ('confirm.php'); 
        exit();
    }
    

    【讨论】:

      【解决方案2】:

      你应该重定向到confirm.php,而不是包含它

      <?php
      if ($_SERVER["REQUEST_METHOD"] == "POST")
      {
          include('validate.php');
      
          if($valid) {
              header('Location: confirm.php'); 
          }
      }
      function test_input($data) {
               $data = trim($data);
               $data = stripslashes($data);
               $data = htmlspecialchars($data);
               return $data;
          }
      ?>
      
      
      
      <!DOCTYPE html >
      <html>
      // all html code with banner and navigation and form elements.
      

      编辑

      当然,这仅在您尚未向浏览器输出任何内容时才有效(IE:echo 'Please fill out the form correctly';

      【讨论】:

      • 是的,我试过了,它可以工作,但是我必须使用会话,这是重定向的唯一方法,没有办法包含它并处理这个
      • 当然,有几种方法可以给猫剥皮。包括退出();在包含 common.php 之后或进行测试以确定您是否应该显示表单或确认文本。
      【解决方案3】:

      您可以将页面重定向到confirm.php,或者如果您想包含confirm.php 而不是你需要在 include ('confirm.php') 行之后放置 exit() 或 die();

      <?php
      if ($_SERVER["REQUEST_METHOD"] == "POST")
      {
          include('validate.php');
      
          if($valid) {
              require_once ('getDBconnection.php');        
              include ('confirm.php'); 
              exit(); //or you can also use die();
          }
      }
      function test_input($data) {
               $data = trim($data);
               $data = stripslashes($data);
               $data = htmlspecialchars($data);
               return $data;
          }
      ?>
      

      【讨论】:

        【解决方案4】:

        这是因为enroll.php无论是否包含confirm.php都会运行到文件末尾。

        你最好将表单提交到confirm.php,然后在confirm.php中重定向用户

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-29
          • 2012-04-25
          • 2017-09-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-10
          • 1970-01-01
          相关资源
          最近更新 更多