【问题标题】:How to redisplay blank html form in php after validation on submit fails. Without using header redirect?提交验证失败后如何在 php 中重新显示空白 html 表单。不使用标头重定向?
【发布时间】:2017-08-11 08:33:28
【问题描述】:

我是 php 新手,这是我的第一篇文章,我对我的编码格式和草率表示歉意。我不确定如何发布代码。但我相信我的问题相当简单。

我正在学习 php OOP 并尝试在 PHP 中重新显示一个 html 表单,该表单是使用 method='POST' 从对象类创建的对象函数 displayContactForm() 调用的,该对象类具有其他各种成员函数,这些函数只是简单地回显 html标记并显示带有 html 表单的简单 `contact.php 页面。

我要做的就是在用户点击提交并且验证失败后重新显示带有空白值的表单,以便表单基本上被清除并且用户必须重新输入信息。我写了一个if/else 语句来处理提交,它将检查是否设置了$_POST,并且字段是!empty,(否则)“以某种方式重新显示已清除的表单(或)清除表单”?我在下面发布了我的 php 页面的代码。 if-else 语句是第 31-38 行。我不能使用标题重定位。我应该用于这个项目的 else 语句的建议行是 $this->displayContactForm($this->form);重新显示表单。但它只是复制了页面上原始表单下方的表单,我最终得到了两个表单?我很困惑?...任何帮助表示赞赏。

<?php
require('./PageLib.php');
require('./ContactFormLib.php');

//created a class called ContactPage that extends class Page
class ContactPage extends Page{
    public $form;
    public function __construct($arg=NULL) {
        $this->form = new ContactForm(); 
    }

    //OVERRIDDEN Page::displayPage()
    public function displayPage(){
        echo "<html>\n<head>\n";
        //HEAD/////////////////

        $this->displayTitle();
        $this->displayStyles();
        echo "</head>\n<body>\n";

        //HEADER///////////////

        $this->displayHeader();
        $this->displayNav($this->navLinks);

        //CONTENT//////////////

        $this->displayContactForm($this->form);
        echo $this->content;
        $this->displayFooter();

        if(isset($_POST['name'], $_POST['phone'], $_POST['email']) && !empty($_POST['name']) && !empty($_POST['phone']) &&     !empty($_POST['email'])){
            echo " Checked form values. Now echoing content to page.";
        }
        else {
            $this->displayContactForm($this->form);        
        }      
        echo "\n</body>\n</html>\n";

        public function displayContactForm($f) {
            $f->displayForm(); 
        }

    }
    $contactpage = new ContactPage();
    $contactpage->pageTitle = "Contact Page";
    $contactpage->headerTitle = "Contact - OOP php Page Builder";
    $contactpage->content="<h3>Contact Page Content</h3>\n";
    $contactpage->displayPage();
?>

【问题讨论】:

    标签: php forms oop refresh


    【解决方案1】:

    仅使用 PHP 是不可能的。您必须结合使用 Ajax 和 PHP 来执行此任务。 PHP 本身需要刷新页面以“重新显示”内容。

    【讨论】:

      猜你喜欢
      • 2014-04-08
      • 2013-11-05
      • 2014-10-04
      • 2010-12-30
      • 2015-09-30
      • 2018-04-10
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多