【问题标题】:Sending values through URL and redirect to another url通过 URL 发送值并重定向到另一个 url
【发布时间】:2019-01-29 12:02:15
【问题描述】:

我正在使用 codeigniter 处理数据。我想更新数据表中的值。我被困在下面的是我的代码。

下面是我的视图代码,其中单击编辑按钮后,我已将 id 值传递给控制器​​应用程序中的 edituser 函数。

<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>

所以它返回一个 404 页面,这个 url “http://localhost/website/app/app/edituser/1” 它正在返回 id 但我想通过我的控制器重定向到另一个页面。

下面是我的控制器代码。

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);
}

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    像这样在application/config/autoload.php 文件中添加 URL 助手

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

    假设您已正确创建 .htaccess 文件,并且 index.php 已从您的 URL 中删除。

    现在使用base_url() 从视图中调用控制器函数

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

    【讨论】:

      【解决方案2】:

      试试这个: id ?>">编辑 或者 ID'); ?>">编辑

      【讨论】:

        【解决方案3】:
        <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"
        }
        

        【讨论】:

          【解决方案4】:

          我没有在这里得到真正的问题。为了使它工作,你需要在你的 Apache 服务器上启用一个 mod_rewrite 的 .htaccess 文件,我猜你有。当使用这样的 URL 时,您应该有某种 URIDispatcher 类来清楚地查看请求 URL 的每个细节。所以,这里的事情是你检查了一些事情,然后根据你的代码如何响应它们来纠正它们:

          URI 格式是否正确?

          首先,在你的Controller脚本上,检查REQUEST_URI是否一切正确,在脚本的开头键入:

          <?php
          
          die($_SERVER['REQUEST_URI']);
          

          URI 格式正确吗?提取ID

          由于您使用的是不带参数的 URL (base_uri?id=1),所以我们使用 explode() 方法来分隔孔 URI:

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

          由于您的 URI 是 /website/app/app/edituser/1,您现在将拥有一个位置为 0 的数组,其中包含一个空字符串。这是正常行为,只需使用$final_uri_array = array_filter($uri_array);注意 array_filter() 如果 ID 为零,则删除它。。如果一切正常,ID 应该是$final_uri_array[4]。现在让我们检查一下您的控制器。

          检查值和重定向

          您的控制器上的代码可能是这样的:

          <?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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-01-10
            • 1970-01-01
            • 2013-05-28
            • 2016-05-24
            • 1970-01-01
            • 2016-06-26
            • 2020-01-24
            • 1970-01-01
            相关资源
            最近更新 更多