【问题标题】:Page redirection in CodeigniterCodeigniter 中的页面重定向
【发布时间】:2014-03-01 04:39:33
【问题描述】:

我是 Codeigniter 的新手。作为研究的一部分,我创建了一个将数据插入数据库的表单。插入后,它没有正确重定向到表单视图页面 (formvalidation.php)。

表单操作

    public function submitform()
    {
        $formdata['username']=$this->input->post('username');
        $formdata['address']=$this->input->post('address');
        $formdata['state']=$this->input->post('state');
        $formdata['country']=$this->input->post('country');
        $data['state']=$this->getstatearray();
        $data['country']=$this->getcountries();
        if($this->userprofile_model->setdata($formdata)=='success')
        {
            $this->load->view('formvalidation/formvalidation',$data);
        }
        else
        {
            $this->load->view('formvalidation/formvalidation',$data);
        }
    }

表单查看页面(formvalidation.php)

echo form_open('formvalidation/submitform'); 
echo form_input('username');
echo "<br>".form_textarea("address");
echo "<br>".form_dropdown('state',$state);
echo "<br>";
echo form_dropdown('country',$country);
echo "<br><br>".form_submit('submit','Submit');
echo form_close();

模型(userprofile_model.php)

class userprofile_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
    }

    public function setdata($data)
    {
        $this->db->insert('userprofile',$data);  
        if($this->db->affected_rows()>0)
        {
            echo "success";
        }
        else 
        {
            echo "fail";
        }
    }
}

现在值被插入到数据库中。但插入后我想将其重定向到 url http://www.example.com/codeigniter/index.php/formvalidation/。但相反,它现在重定向到http://www.example.com/codeigniter/index.php/formvalidation/submitform,其中 submitform 是表单操作方法。

我该如何解决这个问题?我尝试在页面重定向功能之后放置exit;。但它不会起作用。请帮我解决这个问题。

提前致谢。

【问题讨论】:

标签: php codeigniter


【解决方案1】:

是的,因为没有重定向代码。提交表单后,您的 submitform 函数将被执行,并且在该函数中您正在加载视图。所以如果你想重定向 到别的东西你必须使用redirect()函数。

public function submitform()
    {
        $formdata['username']=$this->input->post('username');
        $formdata['address']=$this->input->post('address');
        $formdata['state']=$this->input->post('state');
        $formdata['country']=$this->input->post('country');
        $data['state']=$this->getstatearray();
        $data['country']=$this->getcountries();
        if($this->userprofile_model->setdata($formdata)=='success'){
                 redirect("Your URL");
        }
        else{
                redirect("Your URL");
        }
    }

您还必须正确清理用户输入。您必须先验证表单输入,然后再将其传递给模型。你可以找到更多信息here

【讨论】:

    【解决方案2】:

    首先在控制器索引函数中加载视图页面。

    function index()
    {
    $this-&gt;load-&gt;view('v_bank_master');
    }

    然后在模型页面中,插入完成后。给出下面的代码。

    redirect('give your controller page name here','refresh');

    您的页面现在将在插入后重定向到查看页面。

    希望对你有帮助……

    【讨论】:

      【解决方案3】:

      我使用重定向('yourControllerName', 'refresh')。

      在此处阅读更多信息:Redirect()

      【讨论】:

        【解决方案4】:

        插入后给出以下代码

        redirect('controller_name/function_name');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-07
          • 1970-01-01
          • 1970-01-01
          • 2016-03-23
          • 2014-06-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多