doujujian0052 2013-11-03 12:34
浏览 39
已采纳

如何将数据从表单发送到控制器

In my web store, built on OpenCart, I've created absolutely new template and controller for it. Path is template/common/orderForm. In this template there is a very simple contact form.

Template code (not very much, I didn't even include the header).

<!doctype html>

<div style="width: 723px;">
<form action="<?php echo $action; ?>" method="post">
Ваше имя: <input type="text" name="your_name"><br>
Ваше e-mail: <input type="text" name="email"><br>
<input type="submit" value="Заказать">
</form>
</div>

Well, the biggest problem is that I don't know how to send data from form to controller. All I need to understand is how to do that. The other part, like sending form data via email, I can handle myself. Honestly, I totally can't understand OpenCart system.

Controller code

<?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->children = array(
'common/column_left',
'common/column_right',
'common/content_top',
'common/content_bottom',
'common/footer',
'common/header'
);

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


/* Input data check */
$your_name = $this->config->get('your_name');
echo $your_name;
$email = htmlspecialchars($_POST["email"]);
/* Устанавливаем e-mail адресата */
$myemail = "the.eolithic@gmail.com";
/* Создаем новую переменную, присвоив ей значение */
$message_to_myemail = "Здравствуйте!
Вашей контактной формой было отправлено сообщение!
Имя отправителя: $your_name
E-mail: $email
Конец";
/* Отправляем сообщение, используя mail() функцию */
$from  = "From: $yourname <$email> 
 Reply-To: $email 
";
mail($myemail, $message_to_myemail, $from);
?>
<p>Ваше сообщение было успешно отправлено!</p>
<p>На <a href="index.php">Главную >>></a></p>
<?php
/* Если при заполнении формы были допущены ошибки сработает
следующий код: */
function check_input($data, $problem = "")
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}
function show_error($myError)
{
    ?>
    <html>
    <body>
    <p>Пожалуйста исправьте следующую ошибку:</p>
    <?php echo $myError; ?>
    </body>
    </html>
    <?php
    exit();
}
}
}
?>

Everytime I go to the form page, I get these errors. And I don't even press the submit button.

Notice: Undefined variable: yourname in C:\apache\localhost\www\webshop.kg\catalog\controller\common\orderForm.php on line 40Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:\apache\localhost\www\webshop.kg\catalog\controller\common\orderForm.php on line 41

Thank you for your attention. I hope you know how to resolve my problem.

  • 写回答

1条回答 默认 最新

  • dtdvlazd56637 2013-11-03 20:56
    关注

    Ok, try this...

    your template file should be this:

    <!doctype html>
    
    <div style="width: 723px;">
    <form action="<?php echo $action; ?>" method="post">
    Your name: <input type="text" name="your_name"><br>
    Your e-mail: <input type="text" name="email"><br>
    <input type="submit" value="Order">
    </form>
    </div>
    

    This has just been changed to english....

    Your controller file should be this (I have changed some of it to english)

    <?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->children = array(
    'common/column_left',
    'common/column_right',
    'common/content_top',
    'common/content_bottom',
    'common/footer',
    'common/header'
    );
    
    $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"]);
    echo $your_name;
    $email = htmlspecialchars($_POST["email"]);
    /* Set the e-mail recipient */
    $myemail = "the.eolithic@gmail.com";
    /* Create a new variable by assigning a value to it */
    $message_to_myemail = "Hello!
    Your contact form has been sent a message!
    Sender's name: $your_name
    E-mail: $email
    end";
    /* Send a message using the mail () function */
    $from  = "From: $your_name <$email> 
     Reply-To: $email 
    ";
    mail($myemail, $message_to_myemail, $from);
    ?>
    <p>Your message has been successfully sent!</p>
    <p>At <a href="index.php">Home >>></a></p>
    <?php
    /* If you are filling out the form mistakes were made work
    the following code: */
    function check_input($data, $problem = "")
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        if ($problem && strlen($data) == 0)
        {
            show_error($problem);
        }
        return $data;
    }
    function show_error($myError)
    {
        ?>
        <html>
        <body>
        <p>Please correct the following error:</p>
        <?php echo $myError; ?>
        </body>
        </html>
        <?php
        exit();
    }
    }
    }
    }
    ?>
    

    Let me know if that helps...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常