【问题标题】:Store value to variable Opencart将值存储到变量 Opencart
【发布时间】:2016-05-10 23:05:59
【问题描述】:

我认为这是打开购物车的基本问题,但我无法解决它,鉴于我放置了 1 个字段 head_text_field 然后我将变量放入控制器中,然后使用 var_dump 进行检查值

这个用于查看:

<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
        <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>

  <?php if ($error_warning) { ?>
    <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>

  <?php if ($success) { ?>
    <div class="success"><?php echo $success; ?></div>
  <?php } ?>

    <div class="box">
        <div class="heading">
            <h1><img src="view/image/product.png" alt="" /> <?php echo $heading_title; ?></h1>
            <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a href="<?php echo $cancel; ?>" class="button"><?php echo $button_cancel; ?></a></div>
        </div>

        <div class="content">
          <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
            <table class="form">
                <tr>
                    <td><span class="required">*</span> <?php echo $entry_head; ?></td>
                    <td><input type="text" name="head_text_field" value="<?php echo $head_text_field; ?>" placeholder="Input Head Text" size="40"/></td>
                </tr>
            </table>
          </form>
        </div>
    </div>
</div>
<?php echo $footer; ?>

控制器:

<?php 
class ControllerItemItem extends Controller {
private $error = array(); 

public function index() {
    $this->language->load('item/item');
    $this->document->setTitle($this->language->get('heading_title')); 
    $this->getList();
}

protected function getList(){
    if (isset($this->request->get['head_text_field'])){
        $head_text_field = $this->request->get['head_text_field'];
        var_dump($head_text_field); exit; // VAR_DUMP HERE
    } else {
        $head_text_field = null;
        echo "FAILED";                // FAILED HERE
    }

    $this->data['breadcrumbs'] = array();
    $this->data['breadcrumbs'][] = array(
        'text'      => $this->language->get('text_home'),
        'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
        'separator' => false
    );

    $this->data['breadcrumbs'][] = array(
        'text'      => $this->language->get('heading_title'),
        'href'      => $this->url->link('module/item', 'token=' . $this->session->data['token'], 'SSL'),
        'separator' => ' :: '
    );

    $this->data['heading_title'] = $this->language->get('heading_title');
    $this->data['entry_head'] = $this->language->get('entry_head');
    $this->data['button_save'] = $this->language->get('button_save');
    $this->data['button_cancel'] = $this->language->get('button_cancel');


    $this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
    $this->data['action'] = $this->url->link('item/item/insert', 'token=' . $this->session->data['token'], 'SSL');
    $this->data['token'] = $this->session->data['token'];

    $this->template = 'item/item.tpl';
    $this->children = array(
        'common/header',
        'common/footer'
    );      
    $this->response->setOutput($this->render());            
}

    public function insert()
{
    var_dump($head_text_field); exit;
}
}
?>

当我尝试输入时,结果是 FAILED??我哪里做错了?对于模型,我现在不在控制器中调用或使用它。

编辑 1 抱歉,我添加了函数插入以使其不会出错(用于控制器底部的按钮插入)

【问题讨论】:

  • 您的插入函数是否调用表单操作?
  • 是的,它调用插入,但鉴于我调用按钮保存
    我添加了函数 insert() 来工作,如果我不使用它将找不到页面。

标签: php variables opencart store


【解决方案1】:

$this->request->post['head_text_field']

【讨论】:

  • 很抱歉,这是我的错误。如果head_text_field不存在,调用此代码将返回空,所以它应该是code if (!empty($this->input->post(' head_text_field')))。
  • 和 hardik 一样,它的错误结果是致命错误:Can't use method return value in write context in C:\xampp\htdocs\OC\admin\controller\item\item.php on line 17这是帖子('head_text_field')。
  • 打开购物车,哇...这是我的错误。我作为 CI。它应该是 code isset($this->request->post['head_text_field'])
  • 感谢您帮助我:D
【解决方案2】:

如果您想在视图中从opencart controller 获取任何variable,则需要传递此变量,如下所示:

控制器文件

if (isset($this->request->post['head_text_field'])){
        $this->data['head_text_field'] = $this->request->get['head_text_field']; // Your variable
        var_dump($this->data['head_text_field']); exit; // VAR_DUMP HERE
    } else {
        $this->data['head_text_field'] = null;
        echo "FAILED";                // FAILED HERE
    }

现在您可以在视图文件中以$head_text_field 的形式获得head_text_field

编辑

public function insert()
{
    if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
        echo $this->request->post['head_text_field']; // Your field value
    }
}

【讨论】:

  • 我尝试复制您的代码结果是致命错误:无法在第 17 行的 C:\xampp\htdocs\OC\admin\controller\item\item.php 的写入上下文中使用方法返回值这是帖子('head_text_field')。
  • 调用函数 insert() { var_dump($head_text_field);出口; } 结果为 NULL
  • 您的表单操作 URL 是什么?
  • 这是 $this->data['action'] = $this->url->link('item/item/insert', 'token=' . $this->session->data ['令牌'],'SSL');用于在控制器中插入调用函数。公共函数 insert() { var_dump($head_text_field);出口; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多