cassati 发表于 2011-10-12 16:11:18

要调用某控制器的方法,视图文件中的action应该怎么写?

本帖最后由 cassati 于 2011-10-12 16:29 编辑

请问在以下的代码结构中,视图文件中的action该怎么写呢?

我这样写的话,在编辑页面点“确定”时,得到的是SQL错误:不存在"ctrl_edit"列。错误语句是:“select code, name from warehouse_type where code = ctrl_edit”。

也就是说,此时并没有调用该控制器的edit()方法,却以“ctrl_edit”为参数调用了index($code)方法。

文件结构:
controllers\wh_type\ctrl_edit.php
views\wh_type\view_edit.php


// 控制器:ctrl_edit.php

class ctrl_edit extends CI_Controller {
      public function index($code)
      {
                $this->load->database();
                $sql = "select code, name from warehouse_type where code = " . $code;
                $query = $this->db->query($sql);
                $res['data'] = $query->result_array();
                $this->load->view('wh_type/view_edit', $res);
      }
      public function edit()
      {               
                $code = $this->input->post('code');
                $name = $this->input->post('name');
                $sql = "update warehouse_type set name = '" . $name . "' where code = " . $code;
                echo $sql;
                exit;
      }
}
// 视图:view_edit.php
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
<script type="text/javascript">
      function edit() {
                frm.submit();
                window.opener.location.reload();
      }      
</script>
</head>
<body>
<table border="0" cellpadding="4" cellspacing="0">
<form name="frm" action="ctrl_edit/edit" method="post">
<tbody>
      <tr>
         <td align="right">代码:</td>
          <td align="left">
         <input type="text" id="code" name="code" value="<?php echo $data['code'] ?>" readonly></input>
         </td>
       </tr>
       <tr>
      <td align="right">名称:</td>
      <td align="left">
         <input type="text" id="name" name="name" value="<?php echo $data['name'] ?>"></input>
         </td>
         </tr>
         <tr>
         <td colspan="2" align="middle">
            <input type="button" id="btn_ok" name="btn_ok" value="确定"></input>
         </td>
          </tr>
</tbody>
</form>
</table>
</body>
</html>




cassati 发表于 2011-10-12 16:55:04

使用action="../../ctrl_edit/edit"已解决。

问题可能是由于我在其他页面以"ctrl_edit/index/20"为地址打开新窗口,此时调用了ctrl_edit的index($code)方法。

所以如果在该新窗口中写action目标时,就需要回退两级,重新回到ctrl_edit控制器的目录。

具体什么原因还不懂。。

水月刀猪 发表于 2011-10-12 16:57:59

类似的问题 还不如用site_url() 解决

cassati 发表于 2011-10-12 17:00:12

水月刀猪 发表于 2011-10-12 16:57 static/image/common/back.gif
类似的问题 还不如用site_url() 解决

刚接触CI,还请多指教啊

斯达客 发表于 2011-10-12 18:01:59

<form name="frm" action="ctrl_edit/edit" method="post">如果你没有做伪静态 那么你这里是否有问题呢   有请:http://codeigniter.org.cn/user_guide/helpers/form_helper.html
页: [1]
查看完整版本: 要调用某控制器的方法,视图文件中的action应该怎么写?