提交表单后更改页面内容

时间:2013-11-12 22:05:35

标签: php html forms

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

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

感谢您的关注。

2 个答案:

答案 0 :(得分: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 ! 

答案 1 :(得分:0)

目前,您总是从控制器渲染表单。如果您希望它只显示消息而不是提交表单,那么您只需要在isset($ _ POST [&#39; your_name&#39;])下呈现表单。像这样:

<?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());
    }
}
}
?>