【问题标题】:Change page content after submitting form提交表单后更改页面内容
【发布时间】:2013-11-12 22:05:34
【问题描述】:

所以我用谷歌搜索了我的问题,但没有找到答案。

我有一个带有表单的页面。表格很简单,客户需要输入他的姓名和电话号码。提交表单后,我的控制器通过电子邮件发送客户的姓名和电话号码。
表单提交后,页面出现“您的消息发送成功!”消息,但它出现在页面顶部,并且某些 .css 样式被覆盖。
那么,如何在提交后完全清除页面内容,并用其他内容填充页面? 表格

<form action="<?php echo $action; ?>" method="post" name="orderForm">
    <input type="hidden" name="product_model" value="<?php echo $product_model; ?>">
    <table>
        <tr>
            <td class="right">Ваше имя:</td>
            <td><input type="text" name="your_name"></td>
        </tr>
        <tr>
            <td class="right">Номер телефона:</td>
            <td><input type="text" name="email"></td>
        </tr>
        <tr>
            <td></td>
            <td class="left"><input type="submit" value="ЗАКАЗАТЬ"></td>
        </tr>
    </table>
</form>

控制器

<?php
class ControllerCommonOrderForm extends Controller {
    public function index() {
        $this->document->setTitle($this->config->get('config_title'));
        $this->document->setDescription($this->config->get('config_meta_description'));
        $this->data['action'] = $this->url->link('common/orderForm');
        $this->data['heading_title'] = $this->config->get('config_title');

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/orderForm.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/common/orderForm.tpl';
        } else {
            $this->template = 'default/template/common/orderForm.tpl';
        }


        $this->response->setOutput($this->render());

        /* Check if form has been submitted */
        if( isset($_POST['your_name']) )
        {
            /* Input data check */
            $your_name = htmlspecialchars($_POST["your_name"]);
            $email = htmlspecialchars($_POST["email"]);
            $product_model = htmlspecialchars($_POST["product_model"]);
            /* Set the e-mail recipient */
            $myemail = "imakenza@yandex.ru";
            /* Create a new variable by assigning a value to it */
            $message_to_myemail = "Хозяин, тут заказ пришел.";
            /* Send a message using the mail () function */
            $from  = "Имя клиента: $your_name \r\nТелефон клиента: $email \r\n Модель продукта: $product_model \r\n";
            mail($myemail, $message_to_myemail, $from);
            ?>
            <p>Your message has been successfully sent!</p>
        <?php
        }
    }
}
?>

感谢您的关注。

【问题讨论】:

    标签: php html forms


    【解决方案1】:

    当您说控制器时,您的意思是 MVC 模式。 不要在控制器中输出 HTML!

    此外,您的问题是您要求输出表单然后显示消息:

     $this->response->setOutput($this->render());
    
            /* Check if form has been submitted */
            if( isset($_POST['your_name']) )
            {
                /* Input data check */
                $your_name = htmlspecia
                ...
    

    这样做:

        /* Check if form has been submitted */
        if( !isset($_POST['your_name']) )
        {
                $this->response->setOutput($this->render());
        else
        {
            //$your_name = htmlspecia
            //call an other template here ! 
    

    【讨论】:

      【解决方案2】:

      目前,您总是从控制器呈现表单。如果您希望它只显示消息而不是提交时的表单,那么您只需要不呈现表单 if isset($_POST['your_name'])。像这样:

      <?php
      class ControllerCommonOrderForm extends Controller {
      public function index() {
          $this->document->setTitle($this->config->get('config_title'));
          $this->document->setDescription($this->config->get('config_meta_description'));
          $this->data['action'] = $this->url->link('common/orderForm');
          $this->data['heading_title'] = $this->config->get('config_title');
      
          if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/orderForm.tpl')) {
              $this->template = $this->config->get('config_template') . '/template/common/orderForm.tpl';
          } else {
              $this->template = 'default/template/common/orderForm.tpl';
          }
      
      
      
      
          /* Check if form has been submitted */
          if( isset($_POST['your_name']) )
          {
              /* Input data check */
              $your_name = htmlspecialchars($_POST["your_name"]);
              $email = htmlspecialchars($_POST["email"]);
              $product_model = htmlspecialchars($_POST["product_model"]);
              /* Set the e-mail recipient */
              $myemail = "imakenza@yandex.ru";
              /* Create a new variable by assigning a value to it */
              $message_to_myemail = "Хозяин, тут заказ пришел.";
              /* Send a message using the mail () function */
              $from  = "Имя клиента: $your_name \r\nТелефон клиента: $email \r\n Модель продукта: $product_model \r\n";
              mail($myemail, $message_to_myemail, $from);
              ?>
              <p>Your message has been successfully sent!</p>
          } else {
              $this->response->setOutput($this->render());
          }
      }
      }
      ?>
      

      【讨论】:

      • 我同意@OlivierH。但是,您不应该像那样正常显示文本。而是渲染一个发送的模板。上述方法将作为一个快速修复虽然
      猜你喜欢
      • 2012-07-12
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 2013-10-22
      • 1970-01-01
      相关资源
      最近更新 更多