pjason66 发表于 2015-4-22 11:28:56

Undefined variable错误如何解决?

本帖最后由 Closer 于 2015-4-22 11:38 编辑

<form action="<?php echo site_url('admin/article/edit') ?>" method="POST" enctype="multipart/form-data">
<table class="table">
    <tr >
      <td class="th" colspan="10">编辑文章</td>
    </tr>
    <tr>
      <td>标题</td>
      <td>
            <input type="hidden" name="aid" value="<?php echo $article['aid'] ?>">
            <input type="text" name="title" value="<?php echo $article['title'] ?>"/>
            <?php echo form_error('title', '<span>', '</span>') ?>
      </td>
    </tr>
    <tr>
      <td>类型</td>
      <td>
            <input type="radio" name="type" value="0" <?php if ($article['type'] == 0) echo("checked"); ?>/> 普通
            <input type="radio" name="type" value="1" <?php if ($article['type'] == 1) echo("checked"); ?>/> 热门
      </td>
    </tr>
    <tr>
      <td>栏目</td>
      <td>
            <select name="cid" id="">
                <option><?php echo $article['cname']?></option>
                <?php foreach($category as $v): ?>
                <option value="<?php echo $v['cid'] ?>"<?php echo set_select('cid', $v['cid'])?>><?php echo $v['cname'] ?></option>
                <?php endforeach ?>
            </select>
      </td>
    </tr>
    <tr>
      <td>缩略图</td>
      <td>
            <input type="file" name="thumb"/> <span>当前缩略图:<img src="<?php echo base_url() . 'uploads/'. $article['thumb']?>" /></span>
      </td>
    </tr>
    <tr>
      <td>摘要</td>
      <td>
            <textarea name="info" id="" style="width:550px;height:160px;"><?php echo $article['info'] ?></textarea><?php echo form_error('info', '<span>', '</span>') ?>
      </td>
    </tr>
    <tr>
      <td>内容</td>
      <td>
            <textarea name="content" id="content" style="width:550px;height:500px;"><?php echo $article['content'] ?></textarea>
            <?php echo form_error('content', '<span>', '</span>') ?>
      </td>
    </tr>
    <tr>
      <td colspan="10"><input type="submit" class="input_button" value="发布"/></td>
    </tr>
</table>
</form>

提交时总报以下错误:
A PHP Error was encounteredSeverity: Notice
Message: Undefined variable: article
Filename: admin/edit_article.html
Line Number: 32

">
A PHP Error was encounteredSeverity: Notice
Message: Undefined variable: article
Filename: admin/edit_article.html
Line Number: 33

"/>


初学CI框架,求解原因?

pjason66 发表于 2015-4-22 14:46:05

本帖最后由 pjason66 于 2015-4-22 14:48 编辑

public function edit(){
                $this->load->library('form_validation');
                $status = $this->form_validation->run('article');

                if($status){
                        $this->load->model('article_model', 'art');
                        $aid= $this->input->post('aid');

                        $data = array(
                                'title'        => $this->input->post('title'),
                                'type'        => $this->input->post('type'),
                                'cid'        => $this->input->post('cid'),
                                'thumb'        => $info['file_name'],
                                'info'        => $this->input->post('info'),
                                'content'=> $this->input->post('content'),
                                'time'        => time()
                                );       

                        $data['article'] = $this->art->up_article($aid,$data);
                        success('admin/article/index', '发表成功');
                } else {
                        $this->load->helper('form');
                        $this->load->view('admin/edit_article.html');
                }
        }

提交后动作,不知道哪里有问题?

Closer 发表于 2015-4-22 14:53:20

pjason66 发表于 2015-4-22 14:46
public function edit(){
                $this->load->library('form_validation');
                $status = $this->form_validatio ...
你該看的是出錯誤的 edit_cate.html
話說 view 不是都用 PHP 嗎 ? 第一次看用 html 的

Closer 发表于 2015-4-22 14:57:46

本帖最后由 Closer 于 2015-4-22 15:02 编辑

我有注意到你用 form_validation
若你失敗後導向 $this->load->view('admin/edit_article.html');
但你這個頁面有使用 $data['article'] = $this->art->up_article($aid,$data);
也許你在 view 端有取 $article 這個變量
如果拿不到,就該做一個 isset() 的判斷
 

Closer 发表于 2015-4-22 11:41:42

不是寫了沒取到 $article 的變數值?

pjason66 发表于 2015-4-22 14:32:04

值是取到了的,并且显示出来了,但提交的时候就报这个错误

Closer 发表于 2015-4-22 14:37:51

那就表示你提交過去的網址 site_url('admin/article/edit')
裡面有用到一樣的名稱,然後沒取到這個變數的數據

pjason66 发表于 2015-4-22 16:50:38

就是form_validation出的问题,添加时验证没有问题,但变成修改时的验证就出了问题,现在我也没有找到解决办法

Closer 发表于 2015-4-22 16:58:41

pjason66 发表于 2015-4-22 16:50
就是form_validation出的问题,添加时验证没有问题,但变成修改时的验证就出了问题,现在我也没有找到解决 ...

估計不是 form_validation
是你的邏輯問題

你 if 的下區塊沒有設置 $data['article']
卻又想在 vlew 端用 $article 呼叫他
當然會出錯!
 

pjason66 发表于 2015-4-22 17:09:01

设置$data['article']了的,而且数据显示出来了,是提交时验证返回值是false
页: [1] 2
查看完整版本: Undefined variable错误如何解决?