如何使用opencart将一些变量发布到文件中? [英] How do I post some variables to a file using opencart?

查看:99
本文介绍了如何使用opencart将一些变量发布到文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在opencart中发布变量时遇到了一些麻烦.我想做的是从结帐/登录页面上的文本字段中获取两个变量,称为名称和地址.我希望在单击继续按钮时存储在这两个字段中输入的值,然后将其发送到结帐/来宾页面,在此我想回显这些变量.这是我所做的:

I'm having some trouble posting variables within opencart. What I'm trying to do is to grab two variables from text fields on the checkout/login page, called name and address. I want the values entered into these two fields to be stored when the continue button is clicked, and then sent to the checkout/guest page, where i want to echo out these variables. Here is what i have done:

这是我的checkout.tpl文件,在此文件中,我试图将名称和地址变量发送到checkout/guest页面,尤其是接收方法:

Here is my checkout.tpl file, where I am attempting to send the name and address variables to the checkout/guest page, specifically to the receive method:

$('#button-account').live('click', function() {
  var name = $('#name').val();
  var address = $('#address').val();
  $.post('index.php?route=checkout/guest/receive', { name: name, address: address});

});

然后在guest.php控制器文件上,我收到了发布的变量,并将它们存储在2个名为name和address的变量中:

Then on the guest.php controller file, I receive the posted variables, and store them in 2 variables called name and address:

public function receive() {
$name = $this->request->post['name'];
$address = $this->request->post['address'];
}

然后在guest.tpl文件中,将它们回显:

Then on the guest.tpl file, I echo them out:

<?php
echo $name;
echo $address;
?>

加载访客页面时,收到以下错误消息:注意:未定义的变量:C:\ xampp \ htdocs \ catalog \ view \ theme \ default \ template \ checkout \ guest.tpl在第13行的名称注意:未定义的变量:第14行上C:\ xampp \ htdocs \ catalog \ view \ theme \ default \ template \ checkout \ guest.tpl中的地址

When I load the guest page, I get the following error message: Notice: Undefined variable: name in C:\xampp\htdocs\catalog\view\theme\default\template\checkout\guest.tpl on line 13 Notice: Undefined variable: address in C:\xampp\htdocs\catalog\view\theme\default\template\checkout\guest.tpl on line 14.

如果有人可以告诉我如何使此代码正常工作,我将不胜感激.据我所知,变量要么没有被发送到正确的位置,要么我没有在guest.php页面上正确访问它们.

If anyone can tell me how to make this code work, I would be very grateful. From what I can tell the variables are either not getting sent to the right place, or i am not accessing them correctly on the guest.php page.

推荐答案

首先-我不明白您为什么要从checkout/login页发布一些nameaddress,因为没有这样的字段默认情况下,除非您已添加它们.

First of all - I do not understand why would You like to post some name and address from checkout/login page as there are no such fields at default, unless You have added them.

无论如何,在这种情况下,我将以这种方式进行-像您一样通过AJAX发布到receive()方法.在这里,我将变量保存到会话中:

Anyway in such a case I would proceed this way - post to a receive() method via AJAX as You do. Here I would save the variables into a session:

public function receive() {
    $this->session->data['guest_name'] = $this->request->post['name'];
    $this->session->data['guest_address'] = $this->request->post['address'];
}

现在在catalog/controller/checkout/guest.php中的index方法中检查该会话变量,如果已设置,则将该值存储在$this->data数组中以呈现给模板:

Now in catalog/controller/checkout/guest.php at index method check for that session variables and if set, store the value in the $this->data array for presenting to the template:

if(isset($this->session->data['guest_name'])) { // it is enough to check only for one variable and only if it is set
    $this->data['guest_name'] = $this->session->data['guest_name'];
    $this->data['guest_address'] = $this->session->data['guest_address'];
}

之后,您可以在模板中简单地回显这些值(仍在检查是否存在):

After that You can simply echo these values in Your template (still checking whether exists):

<?php if(isset($guest_name)) { ?>
<div><?php echo $guest_name . ' - ' . $guest_address; ?></div>
<?php } ?>

现在,您应该在避免任何undefined variable通知的情况下完成操作...

Now You should be done while avoiding any undefined variable notices...

这篇关于如何使用opencart将一些变量发布到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆