[英]loading same page in php on basis of query
如果搜索字段留空,我什么也不做,如果输入内容,则加载另一页。 因此,如何加载相同的PHP页面时,输入文本框的值是空白 ,不同的PHP页面时输入的文本值不为空 ...?
例如
<input id="srch" type="text" name="query" >
如果此文本框的值为“”,则应加载相同的php页面,其他页面应加载...。
有什么办法在PHP或JavaScript ..?
在PHP中,在检查查询内容的脚本中,您可以执行以下操作:
if( isset($_GET['query']) && $_GET['query'] != '' ) {
header('Location: someotherpage.php?query=' . urlencode($_GET['query']));
exit();
}
此qill指示浏览器转到someotherpage.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>
在上面的示例中,然后提交了表单,该表单转到javascript中的validate()
函数。 此功能快速检查#srch
是否为空。 如果不是,则会将表单的action
更改为您指定的页面。 我将该值设置为YOUR_OTHER_PAGE
,您将不得不将其更改为适当的页面。 如果#srch
值为空,那么它将保留我设置为PHP_SELF
。
在PHP中-提交表单后-让我们说说一个名为decide.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>
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=" " />
<div class="suggestionsBox" id="suggestions" >
<div class="suggestionList" id="autoSuggestionsList">
</div>
</div>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.