Welcome Guest, Not a member yet? Register   Sign In
Form redirect to URL
#1

[eluser]Unknown[/eluser]
Greetings forum,

I am trying to create a simple form to be redirected to a URL upon submission. However, I need the user to submit a URL and once they press the submit button, their page redirects to the url they submitted. I have had quite the challenge learning how to do this. If anyone could point me in the right direction, it would be appreciated.

Many thanks,

Robert Williams
#2

[eluser]ranjudsokomora[/eluser]
rmw603,
You will need a controller, that may look like this:
Code:
class Welcome extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('url');
        $this->load->helper('form');
    }
    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *         http://example.com/index.php/welcome
     *    - or -  
     *         http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://ellislab.com/codeigniter/user-guide/general/urls.html
     */
    public function index()
    {
        //$this->load->view('welcome_message');
        $this->form_validation->set_rules('field0','URL','required');
        IF ($this->form_validation->run()===FALSE) {
            $this->load->view("form");
        }
        ELSE {
            redirect($this->input->post('field0'));
            exit;
        }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

and your view may look like this:
Code:
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Form Title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?PHP
echo form_open('welcome/index');
?&gt;
<h1>URL:</h1><br />
&lt;?PHP
echo form_input('field0','http://www.google.com');
?&gt;
<br />
&lt;?PHP
echo form_submit('submit','Go To Site!');
echo form_close();
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB