duanmiao6695 2018-02-14 08:59
浏览 30
已采纳

$ _GET变量在MVC设计的代码中的搜索函数中使用时不返回值

This question is similar to this one but a little different. I am trying to create a search feature in my code. However when i use the $_GET variable i am not able to echo-out the search value entered.

Here is the code separated in 3 files(i am using a custom MVC design and not any particular framework)

Search form

<form action = "maina2/getStaffname" method = "get">
        <select name = "choice">
            <option value = "staffname" name = "staffname">title</option>
            <option value = "id" name = "id"> author</option>

        </select>
            <input name= "search" type ="text"  size="65" maxlength = "88" style = "display:inline">
            <input name = "mysearch" type="submit"  style = "display:inline">
</form>

The search controller

<?php

    class Search extends Controller {
        public function __construct(){
        parent::__construct();
        }

        function Index() {
            $this->view->getStaffname = $this->model->getStaffname();
            $this->view->render('search/index');

        }

        function getStaffname() {
            $this->model->getStaffname();

        }
    }

And this is the search model file

<?php

class Search_Model extends Model{
    public function __construct() {
    parent::__construct();
        echo 'This is the SEARCH MODEL CLASS <br />';

    }

    public function getStaffname(){
        if(isset($_GET['search'])) {
                echo $_GET['search'];

        }
    }
}

What surprices me is that when i change the method to POST then i am able to echo out the search value. What might be wrong with my technique?

  • 写回答

1条回答 默认 最新

  • dpl74687 2018-02-14 11:45
    关注

    The rewriterule looks like this RewriteRule ^(.+)$ index.php?url=$1

    You're replacing all your query parameters with one url parameter; if there was any ?search=... in the URL before, it's not going to be there afterwards. You want to preserve existing parameters with QSA:

    RewriteRule ^(.+)$ index.php?url=$1 [QSA]
    

    Why is it when i change the method to 'POST' in the form and use $_POST['search'] in the controller i am able to echo out?

    Because the request body ("POST parameters") isn't being affected by the rewrite rule.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码