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

时间:2013-11-03 12:34:53

标签: php opencart

在我的网上商店中,我建立在OpenCart上,我为它创建了绝对新的模板和控制器。路径是模板/公共/ orderForm。在这个模板中有一个非常简单的联系表格。

模板代码(不是很多,我甚至没有包含标题)。

<!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>

嗯,最大的问题是我不知道如何将数据从表单发送到控制器。我需要了解的是如何做到这一点。另一部分,比如通过电子邮件发送表单数据,我可以处理自己。老实说,我完全无法理解OpenCart系统。

控制器代码

<?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> \r\n Reply-To: $email \r\n";
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();
}
}
}
?>

每次我去表单页面时,都会收到这些错误。我甚至没有按下提交按钮。

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

感谢您的关注。我希望你知道如何解决我的问题。

1 个答案:

答案 0 :(得分:3)

好的,试试这个......

您的模板文件应为:

<!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>

这刚刚改为英语......

你的控制器文件应该是这个(我已将其中一部分更改为英文)

<?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> \r\n Reply-To: $email \r\n";
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();
}
}
}
}
?>

如果有帮助,请告诉我......