如果搜索字段留空,我会尝试什么也不做,如果输入了某些内容,则加载另一页。
所以,当输入文本框值为空白时,如何加载相同的php页面,当输入文本值不为空时,如何
例如。
<input id="srch" type="text" name="query" >
如果此文本框的值为“”,则应加载相同的php页面 否则,其他页面......
php 或 javascript 有什么办法吗?
答案 0 :(得分:0)
在PHP中,在检查查询内容的脚本中,您可以这样做:
if( isset($_GET['query']) && $_GET['query'] != '' ) {
header('Location: someotherpage.php?query=' . urlencode($_GET['query']));
exit();
}
这个qill指示浏览器转到someotherpage.php
答案 1 :(得分:0)
您可以为表单使用简单的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
的操作。
答案 2 :(得分:0)
在PHP中 - 提交表单后 - 让我们说一个名为decide.php
它的内容:
if (!isset($_GET['query']) || $_GET['query']=='' )
include "emptysearchlanding.php";
else
include "somethingelse.php";
答案 3 :(得分:-1)
以下是我使用的代码,您可以通过使用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>