PHP Get表单问题


PHP Get Form issue

我有以下引用:

<form class="searchform" name="search" id="search" action="<?php echo base_url();?>video/search" method="get" >
                <input type="text" name="search_key" onblur="if (this.value == '') {this.value = 'Search video';}" onfocus="if (this.value == 'Search video') {this.value = '';}" value="<?php if(isset($search_key))echo $search_key;else echo 'Search video';?>" class="searchfield" onKeyPress="return submitenter(this,event)">
        </form>

页面url在搜索后看起来像这样-例如:

mypage.com/video/search?search_key=blabla

和我用来展示它的代码是:

<?php echo $_GET["search_key"]; ?>

但它不工作,我不明白为什么。

我不能让-blabla-显示在页面中

你的actionform标签,应该是一个PHP文件像myphp.php,然后你可以使用$_GET[ ["search_key'];at myphp.php '获取。例如

action="<?php echo base_url();?>video/search/myphp.php"

那么URL应该像mypage.com/video/search/myphp.php?search_key=blabla

通过使用var_dump($_GET)查看$_GET超全局变量中是否有任何值,这会将对象的不太漂亮的字符串转储到屏幕上。

也有一个尝试$_REQUEST$GLOBALS变量,看看他们是否有任何值做相同的上面。

如果这些没有显示您的值,您可能需要强制设置表单的enctype,以便PHP知道它可以将查询字符串(从?开始的文本)或POST数据放入超全局变量中。您想要使用的enctypeapplication/x-www-form-urlencoded,这意味着默认值,但以防万一,设置它。

设置后,您的表单将看起来像

<form class="searchform" name="search" id="search" action="<?php echo base_url();?>video/search" method="get" enctype="application/x-www-form-urlencoded">
    <input type="text" name="search_key" onblur="if (this.value == '') {this.value = 'Search video';}" onfocus="if (this.value == 'Search video') {this.value = '';}" value="<?php if(isset($search_key))echo $search_key;else echo 'Search video';?>" class="searchfield" onKeyPress="return submitenter(this,event)" />
</form>
超级全局变量

也只是以防你错过了它,你的input标签需要一个/之前关闭>,像/>,否则它不是有效的标记

如果URL /search?search_key=blabla显示,尝试使用$_POSTprint_r($_POST)捕获,因为CI上的默认表单方法是使用post

要从查询字符串中获得get值,您必须将查询字符串设置为true。

1)应用程序/config/config . php2)设置查询字符串选项true

交货。$config['enable_query_strings'] = true;

$_GET["search_key"]

否则发送像

这样的值
mypage.com/video/search/blabla

您将在视频控制器搜索功能的第一个参数中得到"blabla"