0

I'm new to PHP CodeIgniter and I'm creating my first app, I've created the controller, model and views to add records to my DB and so far so good,

The problem starts when I click 'submit' in my form,

as instead of redirecting to the same form again if there is an issue in the form or redirecting to the success page, it redirects to the same page but with the full path twice,

like this:

form page - localhost/index.php/news/create

expected results:

form data is valid ->localhost/index.php/news/success

form is invalid -> localhost/index.php/news/create (same page)

but instead it does this when I click submit:

localhost/index.php/news/localhost/index.php/news/create

as you can see, it takes the full path again and puts it afterwards the already existing url.

this is my code

routes.php

$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
$route['news/create'] = 'news/create';

main controller

public function view($page = 'home')
    {
            if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
            {
                    // Whoops, we don't have a page for that!
                    show_404();
            }

            $data['title'] = ucfirst($page); // Capitalize the first letter

            $this->load->view('templates/header', $data);
            $this->load->view('pages/'.$page, $data);
            $this->load->view('templates/footer', $data);
    }

controller that has the function create of the form

class News extends CI_Controller {
        public function create()
        {
            $this->load->helper('form');
            $this->load->library('form_validation');

            $data['title'] = 'Create a news item';

            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('text', 'Text', 'required');

            if ($this->form_validation->run() === FALSE)
            {
                $this->load->view('templates/header', $data);
                $this->load->view('news/create');
                $this->load->view('templates/footer');

            }
            else
            {
                $this->news_model->set_news();
                $this->load->view('news/success');
            }
        }
}

model:

public function set_news()
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'text' => $this->input->post('text')
        );

        return $this->db->insert('news', $data);
    }

Form

<h2><?php echo $title; ?></h2>

<?php echo validation_errors(); ?>

<?php echo form_open('news/create'); ?>

    <label for="title">Title</label>
    <input type="input" name="title" /><br />

    <label for="text">Text</label>
    <textarea name="text"></textarea><br />

    <input type="submit" name="submit" value="Create news item" />

</form>
7
  • Could you post the Form-HTML that's being shown in your browser? Commented Jul 21, 2017 at 19:00
  • ======= <html> <head> <title>CodeIgniter Tutorial</title> </head> <body> <h1>Create a news item</h1><h2>Create a news item</h2> <form action="localhost/index.php/news/create" method="post" accept-charset="utf-8"> <label for="title">Title</label> <input type="input" name="title" /><br /> <label for="text">Text</label> <textarea name="text"></textarea><br /> <input type="submit" name="submit" value="Create news item" /> </form> <em>&copy; 2015</em> </body> </html> ====== Commented Jul 21, 2017 at 21:25
  • what's your base_url set to in config.php? Commented Jul 23, 2017 at 22:03
  • 1
    Have you tried $config['base_url'] = 'http://localhost/'; ? Commented Jul 25, 2017 at 11:03
  • 1
    @tobifasc thanks that worked!! Commented Jul 26, 2017 at 18:31

2 Answers 2

1

It's happening because you have already view loaded and when your validation failed again you loaded your same view so you don't need to view again. Replace your News controller the create method by this code EX:

class News extends CI_Controller {
        public function create()
        {
            $this->load->helper('form');
            $this->load->library('form_validation');

            $data['title'] = 'Create a news item';

            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('text', 'Text', 'required');

            if ($this->form_validation->run() === FALSE)
            {

            }
            else
            {
                $this->news_model->set_news();
                $this->load->view('news/success');
            }
        }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Thanks to @tobifasc for the answer,

The problem was in config.php

I replaced

$config['base_url'] = 'localhost'; 

to this:

$config['base_url'] = 'http://localhost/';

and everything is working properly now

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.