0

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

我有一个带有表格的页面。表格很简单,客户需要输入他的姓名和电话号码。提交表格后,我的控制器通过电子邮件发送客户的姓名和电话号码。
表单提交后,页面出现“您的消息发送成功!” 消息,但它出现在页面顶部,并且某些 .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
        }
    }
}
?>

感谢您的关注。

4

2 回答 2

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 ! 
于 2013-11-12T22:11:08.780 回答
0

目前,您总是从控制器呈现表单。如果您希望它只显示消息而不是提交时的表单,那么您只需要不呈现表单 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());
    }
}
}
?>
于 2013-11-12T22:11:54.240 回答