【问题标题】:How to send php variable from view(html) to controller如何将 php 变量从视图(html)发送到控制器
【发布时间】:2016-12-02 14:00:20
【问题描述】:

这是我的 html 代码。我想将“$category_edit->c_name”值传递给我的Update() 控制器。我正在从另一个控制器获取“$category_edit”变量。

我正在使用 CodeIgniter 框架。

<form method="post" action="<?php echo base_url('admin/category/update');?>">
 <label>Parent Category: </label></br>
 <select name="parent_id">
 <?php
    echo '<option value="' .$category_edit->id .'">';
    echo $category_edit->p_name;
    echo '</option>';
 ?>
 </select>
 <label>Category</label>
 <input type="text" name="<?php echo $category_edit->c_name; ?>" id="category_name" value="<?php echo $category_edit->c_name; ?>">
 <button>Update</button>
</form>

这是我的update() 控制器。 我收到错误:

  1. 未定义变量:category_edit
  2. 试图获取非对象的属性

    public function update(){
       $this->load->model('category_model');
       echo $category_edit->c_name;
    }
    

【问题讨论】:

  • $category_edit 这不是全局变量。
  • 检查我的答案兄弟
  • 添加您的控制器代码。 错误是Undefined variable: category_edit。这意味着category_edit 数组为空。

标签: php html codeigniter codeigniter-2


【解决方案1】:

请检查此参考代码:

public function update_view()
{
    $this->load->model('category_model');

    $data['category_edit'] = $this->category_model->get_category_values(); // return array
    $data['extra_variable'] = 'lorem ipsum';

    $this->load->view('category/update_view', $data);
}

在您的category/update_view.php

<form method="post" action="<?php echo base_url('admin/category/update');?>">
 <label>Parent Category: </label></br>
 <select name="parent_id">
 <?php
    echo '<option value="' .$category_edit['id'] .'">';
    echo $category_edit['p_name'];
    echo '</option>';
 ?>
 </select>
 <label>Category</label>
 <input type="text" name="<?php echo $category_edit['c_name']; ?>" id="category_name" value="<?php echo $category_edit['c_name']; ?>">
 <button>Update</button>
</form>

编辑:

请参考:http://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view

【讨论】:

  • 错误是Undefined variable: category_edit。这意味着category_edit 数组为空。你的答案是错误的。它不会打印任何要查看的数据。
  • 老兄,它现在是你的工作,它不能在你做数据库工作时返回空数组。 “或”对其进行验证。 “我可以告诉你路径,不是完整的程序”。我完全正确,这是我项目的同一份副本。
  • 在你看来。看看打印的内容。
【解决方案2】:

在 HTML 中做一些更改(见下面的代码)

<input type="text" name="category_name" id="category_name" value="<?php echo $category_edit->c_name; ?>">

public function update()
{
$this->load->model('category_model');
echo $this->input->post('category_name');
}

【讨论】:

    【解决方案3】:

    您需要添加静态字段的名称,而不是任何变量。 所以尝试像这样添加

    <input type="text" name="category_name" id="category_name" value="<?php echo $category_edit->c_name; ?>">
    

    在控制器上你可以获得它的价值

    $this->input->post('category_name');
    

    【讨论】:

    • 错误是Undefined variable: category_edit。这意味着category_edit 数组为空。你的答案是错误的。它不会打印任何要查看的数据。
    • 这个是controller用的,view要自己搞定。
    • 用户已从视图向控制器询问
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多