基于查询在php中加载同一页面

基于查询在php中加载同一页面,php,javascript,html,Php,Javascript,Html,如果搜索字段留空,我将尝试什么也不做,如果输入了内容,我将加载另一个页面。 那么,当输入文本框值为空白时,如何加载相同的php页面,而当输入文本值为非空白时,如何加载不同的php页面 例如 <input id="srch" type="text" name="query" > 若此文本框的值为“”,则应加载相同的php页面 否则,另一页 在php或javascript中是否有任何方法?在php中,在检查查询内容的脚本中,可以执行以下操作: if( isset($_GET['qu

如果搜索字段留空,我将尝试什么也不做,如果输入了内容,我将加载另一个页面。 那么,当输入文本框值为空白时,如何加载相同的php页面,而当输入文本值为非空白时,如何加载不同的php页面

例如

<input id="srch" type="text" name="query" >

若此文本框的值为“”,则应加载相同的php页面 否则,另一页


phpjavascript中是否有任何方法?

在php中,在检查查询内容的脚本中,可以执行以下操作:

if( isset($_GET['query']) && $_GET['query'] != '' ) {
    header('Location: someotherpage.php?query=' . urlencode($_GET['query']));
    exit();
}

这将指示浏览器转到其他页面。php

您可以为表单使用一个简单的
onsubmit
函数:

<form method="post" action="<?php $_SERVER['PHP_SELF']?>" onSubmit="return validate()" id="searchForm">
    <input id="srch" type="text" name="query" >
    <input type="submit" value="Submit">
</form>

<script type="text/javascript">
function validate(){
    if(document.getElementById('srch').value != ''){
        document.getElementById('searchForm').action = 'YOUR_OTHER_PAGE';
    }
}
</script>

在PHP中-提交表单后-让我们假设一个名为
decise.PHP的脚本

它的内容是:

if (!isset($_GET['query']) || $_GET['query']=='' )
    include "emptysearchlanding.php";
else
    include "somethingelse.php";

下面是我使用的代码,您可以通过使用
inputString.length==0
进行空白输入,然后执行if语句来执行第二个函数。 我在回答中包含的代码用于将输入自动发布到php文件

javascript

<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) 
{
$('#suggestions').hide();
}
else
{
$.post("http://www.example.com/suggest.php", {queryString: ""+inputString+""}, function(data){
if(data.length > 0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} 
function fill(thisValue) {
$('#inputString').val(thisValue);
setTimeout("$('#suggestions').hide();", 200);
}
function outoffocus() {
setTimeout("$('#suggestions').hide();", 200);
}
</script>

函数查找(inputString){
如果(inputString.length==0)
{
$(“#建议”).hide();
}
其他的
{
$.post(”http://www.example.com/suggest.php“,{queryString:”“+inputString+”“},函数(数据){
如果(data.length>0){
$(“#建议”).show();
$('#autoSuggestionsList').html(数据);
}
});
}
} 
函数填充(thisValue){
$('#inputString').val(thisValue);
setTimeout(“$('#建议”).hide();”,200);
}
函数离焦(){
setTimeout(“$('#建议”).hide();”,200);
}
HTML

<form id="search" action="/search.php" method="get"> 
<input type="text" name="search" id="inputString" onkeyup="lookup(this.value);" onblur="outoffocus()" onfocus="lookup(this.value);"/>
<input type="submit" value="&nbsp;" />
<div class="suggestionsBox" id="suggestions" >
 <div class="suggestionList" id="autoSuggestionsList">
&nbsp;
</div>
</div>
</form>

@manoj不客气。请考虑把答案作为正确答案。