Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将变量从控制器传递到视图_Php_Codeigniter - Fatal编程技术网

Php 如何将变量从控制器传递到视图

Php 如何将变量从控制器传递到视图,php,codeigniter,Php,Codeigniter,例如,我的控制器中有这段代码。我想知道如何将$nameErr传递给我的视图HTML $name = test_input($_POST["name"]); // check if name only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } 您没有在代码控制器中指定视

例如,我的控制器中有这段代码。我想知道如何将$nameErr传递给我的视图HTML

 $name = test_input($_POST["name"]);
 // check if name only contains letters and whitespace
 if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
   $nameErr = "Only letters and white space allowed"; 
 }

您没有在代码控制器中指定视图名称

根据:

您必须使用:$this->load->view'view\u name',$nameErr

试试这个

若要提交帖子,您需要codeigniter form_验证,您还可以使用我推荐的回调函数

控制器示例函数窗体验证回调

视图示例

class Login extends CI_Controller {

 function index() {

    $this->load->library('form_validation');

    $this->load->helper('form');
    $this->load->helper('url');

    $this->form_validation->set_rules('name', 'name', 'required|callback_checkname');

    if ($this->form_validation->run() == FALSE) {
        $this->load->view('your_view');
    } else {
        // redirect('success_page');
    }
 }

 function checkname() {

    $this->load->library('form_validation');

    $name = $this->input->post('name');

    if (preg_match("/^[a-zA-Z ]*$/", $name)) {
       $this->form_validation->set_message('checkname' ,"Only letters and white space allowed"); 
       return false;
    }
  }
}

姓名:*很抱歉,您的当前解决方案有什么问题?为什么它不能实现您想要实现的目标?为什么不使用codeigniter表单验证并以这种方式传递错误。不要在codeigniter上使用$\u POST name使用$this->input->POST'name'库表单验证在这里和表单助手谢谢您指出这一点您确定要通过if!preg_match/^[a-zA-Z]*$/,$name{?
 // Don't Use $_POST[] use $this->input->post()

 $name = test_input($_POST["name"]); 

 // check if name only contains letters and whitespace

 if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
   $data['nameErr'] = "Only letters and white space allowed"; 
 }

 $this->load->view('view_name', $data);
class Login extends CI_Controller {

 function index() {

    $this->load->library('form_validation');

    $this->load->helper('form');
    $this->load->helper('url');

    $this->form_validation->set_rules('name', 'name', 'required|callback_checkname');

    if ($this->form_validation->run() == FALSE) {
        $this->load->view('your_view');
    } else {
        // redirect('success_page');
    }
 }

 function checkname() {

    $this->load->library('form_validation');

    $name = $this->input->post('name');

    if (preg_match("/^[a-zA-Z ]*$/", $name)) {
       $this->form_validation->set_message('checkname' ,"Only letters and white space allowed"); 
       return false;
    }
  }
}
<?php echo validation_errors(); ?>
<form action="<?php echo base_url('login');?>" method="post">
<input type="text" name="name" />
<button type="submit">Submit</button>
</form>