繁体   English   中英

Codeigniter set_value不起作用

[英]Codeigniter set_value not working

我正在查询数据库,并从控制器发送一个$ data数组到我的视图,在这里我使用带有set_value(field_name,default)的CI的表单帮助器,但是没有加载数据。

在我看来,这是我目前正在做的事情:

<input type="hidden" id="artist-id" name="record_artist_id" value="<?php echo set_value('record_artist_id', $record_artist_id); ?>">

我以为我必须使用输入助手,所以我尝试了:

<label for="record-name"><span class="required">*</span>Name:</label>
                    <?php
                    echo form_input([
                        'type' => 'text',
                        'name' => 'record_name',
                        'id' => 'record-name' ,
                        'class' => 'form-control',
                        'value' => set_value('record_name')
                    ]);
                    ?>

但仍然无法正常工作。

这是可行的:

<input id="record-name" name="record_name" type="text" value="<?php echo (isset($record_name)) ? $record_name : ''; ?>" class="form-control">

表单助手正在autoload.php中加载

$autoload['helper'] = array('url', 'file', 'form', 'base');

我不知道这是否相关,但是我将视图作为字符串获取,然后将其传递给模板,例如:

public function add_content($view, $content = NULL){
        $this->content = $this->load->view($view, $content, TRUE);
        return $this->content;
    }

然后以另一种方法:

// render content
            $this->load->view('partials/content', ['content' => $this->content]);

关于我在做什么错的任何想法吗?

尝试这个

控制者

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(1);
class Welcome extends CI_Controller {
    public function index()
    {
        $this->load->view('welcome_message');
    }

     public function test()
    {
          $this->load->helper('form','url');
          $this->load->library("form_validation");
//          echo "vijay";
          $post=$this->input->post();
          if($post)
          {
//               echo "<pre>";print_r($post);die;
               $this->form_validation->set_rules('record_name', 'Record Name', 'trim|required');
               $this->form_validation->set_rules('quantity', 'quantity Name', 'trim|required|numeric');
               if($this->form_validation->run()==true)
               {
                    redirect('/');
                    return 0;
               }
          }

          $data['message']=  validation_errors();
        $this->load->view('test',$data);
    }
}

视图

<!DOCTYPE html>
<html lang="en">
     <head>
          <meta charset="utf-8">
          <title>Welcome to CodeIgniter</title>


     </head>
     <body>

          <div id="container">
               <h1>Welcome to CodeIgniter!</h1>

               <div id="body">

               </div>
               <p>
                 <?= !empty($message) ? $message : '' ?>
               </p>
               <form method="post">
                    <label for="record-name"><span class="required">*</span>Name:</label>
                    <?php
                    $record_name_input = array('type' => 'text',
                                             'name' => 'record_name',
                                             'id' => 'record-name',
                                             'class' => 'form-control',
                                             'value' => set_value('record_name')
                    );
                    echo form_input($record_name_input);
                    ?>

                    <h5>Username</h5>
                    <input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />
                    <?php
                    echo form_submit("submit", "submit data");
                    ?>

               </form>
          </div>

     </body>
</html>

直到您不使用$this->form_validation->run()直到set_value('field_name')无效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM