i am facing 404_error when i press the edit button - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: i am facing 404_error when i press the edit button (/showthread.php?tid=74013) |
i am facing 404_error when i press the edit button - tayyab - 07-08-2019 controller: public function edit($slug){ $data['post'] = $this->post_model->get_posts($slug); if (empty($data['post'])) { show_404(); } $data['title']= 'Edit Post'; $this->load->view('templates/header'); $this->load->view('posts/edit', $data); $this->load->view('templates/footer'); } viewedit.php) <h2><?= $title ?> </h2> <?php echo validation_errors(); ?> <?php echo form_open('posts/create'); ?> <input type="text" id="hide" name="did" value="<?php echo $post['id']; ?>"> <div class="form-group"> <label>Title</label> <input type="text" class="form-control"name="title" placeholder="Add Title"> </div> <div class="form-group"> <label >Body</label> <textarea class="form-control" name="body" placeholder="Add Body"> </textarea> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> viewview.php) <h2> <?php echo $post['title']; ?> </h2> <small class="post-date">posted on: <?php echo $post['created_at']; ?> </small> <br> <div class="post-body"> <?php echo $post['body']; ?> </div> <hr> <a class="btn btn-default pull-left" href="<?php echo base_url(); ?>posts/edit/">Edit</a> <br><br> <?php echo form_open('/posts/delete/'.$post['id']);?> <input type="submit" value="delete"class="btn btn-success "> </form> routes: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); $route['posts/create'] = 'posts/create'; $route['posts/delete/(:num)'] = 'posts/delete/$1'; $route['posts/(:any)'] = 'posts/view/$1'; $route['posts'] = 'posts/index'; $route['default_controller'] = 'pages/view'; $route['(:any)'] = 'pages/view/$1'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE; RE: i am facing 404_error when i press the edit button - Wouter60 - 07-08-2019 The edit method in your controller is expecting an argument ($slug). The button in your view doesn't pass any argument: PHP Code: <a class="btn btn-default pull-left" href="<?php echo base_url(); ?>posts/edit/">Edit</a> Load (or autoload) the url helper and change it to: PHP Code: <?= anchor('posts/edit/' . $post['slug'], 'Edit', 'class="btn btn-default pull-left"');?> The anchor function will automatically insert the site_url() (not the base_url()) into the href part. |