code123
code123

Reputation: 39

Sending values through URL and redirect to another url

I am working on datable in codeigniter. I want to update the values in datatable. I am stuck below is my code.

below is my view code in which upon clicking edit button i have passed the id value to my edituser function in controller App.

<table id="user_table" class="display">
<thead>
 <td>Id</td>
 <td>Email</td>
 <td>Password</td>
 <td>Action</td>
</thead>
<tbody>    
 <?php 
 foreach ($rows as $row)
 {
   ?>
 <tr>
  <td>
   <?= $row->id ?>
  </td>      
  <td>
   <?= $row->email ?>
  </td>
  <td>
   <?= $row->password ?>
  </td>

  <td>
<a href="app/edituser/<?php echo $row->id ?>"><button id="btn_edit_user" type="button" class="btn btn-primary">Edit</button></a>
</td>
</table>

so it returns a 404 page with this url "http://localhost/website/app/app/edituser/1" it is returning id but i want to redirect to another page via my controller.

Below is my controller code.

public function edituser(){ 
    $user_id = $this->uri->segment(3);
    //$user_id = $this->input->post("user_id");

    if(!empty($user_id)){
        $data["page"] = "edituser";
        $data["row"] = $this->crud->read_row("*", "user", array("id" => $user_id));
    }
    else{
        $data["page"] = "404";
    }

    $this->load->view("app/index", $data);
}

Upvotes: 0

Views: 66

Answers (4)

Christian
Christian

Reputation: 24

I'm not getting the actual question here. To make that work, you need to have an .htaccess file with mod_rewrite enabled on your Apache Server, which I guess you have. When using URLs like that, you should have some kind of URIDispatcher class to clearly see every detail from the requested URL. So, the thing here is you have check a few things and then correct them depending on how your code responds to them:

Is the URI correctly formed?

First of all, on your Controller script, check if the REQUEST_URI to see if everything is correct, at the beginning of the script type:

<?php

die($_SERVER['REQUEST_URI']);

URI correctly formed? Extract the ID

As you are using an URL with no parameters (base_uri?id=1), let's use the explode() method to separate the hole URI:

$uri_array = explode('/', $_SERVER['REQUEST_URI']);

As your URI is /website/app/app/edituser/1, you will now have an array with a position 0, containing an empty String. This is a normal behaviour, just use $final_uri_array = array_filter($uri_array);. Notice that array_filter() with delete the ID if it's a zero.. If everything is going fine, the ID should be in $final_uri_array[4]. Let's now do a check in your Controller.

Checking value and redirecting

Your code on your Controller could be something like this:

<?php

//Your stuff

public function edituser(){
  $final_uri_array = array_filter( explode('/', $_SERVER['REQUEST_URI']) );

  if(sizeof($final_uri_array) < 5){
    //400 Bad request
    http_response_code(400);
  } else {
    /*
     * Check ID existence from your database
     * Should look womething like this
     */
    $user_id = intval($final_uri_array[4]);
    $user_data = $database->search($user_id);
    if($user_data->num_rows > 0){
      //If the user was found, redirect to a page
      header('Location: /your/edit/view/uri');
    } else {
      //404 Not found
      http_response_code(404)
    }
  }

}

//More stuff

Upvotes: 0

Raghav
Raghav

Reputation: 71

<a href="**<?php echo base_url().index_page()."/app/edituser/".$row['id']?>**">
   <button id="btn_edit_user" type="button"class="btn btn-primary">
     Edit
   </button>
</a>

IN controller you can access this like

function edituser($id=''){
 "your query"
}

Upvotes: 0

Shehbaz Badi
Shehbaz Badi

Reputation: 19

Try this: id ?>">Edit OR id'); ?>">Edit

Upvotes: 0

Danish Ali
Danish Ali

Reputation: 2352

Add URL helper in application/config/autoload.php file like this

$autoload['helper'] = array('url');

Assuming you've created .htaccess file correctly and index.php is removed from your URL.

And now use base_url() to call controller function from view

<a href="<?= base_url('app/edituser/'.$row->id)"><button id="btn_edit_user" type="button" class="btn btn-primary">Edit</button></a>

Upvotes: 1

Related Questions