duanpie2834 2012-12-16 06:38
浏览 115
已采纳

如何使用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:

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

});

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'];
}

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

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

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.

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.

  • 写回答

1条回答 默认 最新

  • doz22551 2012-12-17 09:45
    关注

    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.

    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'];
    }
    

    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 } ?>
    

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

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

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致