duanmiao6695 2018-02-14 00: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 03: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部