2016-08-09 73 views
-1

我有不同领域的搜索形式,但我想设置一个默认到URL如果该字段为空:默认值,如果输入字段为空

<input type="text" name="price-max" class="form-control" placeholder="Max Price" > 

当形式submited我的URL看起来是像这样

search.php?location=location&category=Category&status=lease&price-max= 

相反,我想要一个默认值,以便当我提交一个空的价格字段的网址应该喜欢的形式。

search.php?location=location&category=Category&status=lease&price-max=999 
+0

定义输入字段本身的值 –

+2

做服务器端,像'$ var = empty($ _ GET ['price-max'])? 999:$ _GET ['price-max'];' – FirstOne

+0

是否允许JavaScript? – HerrSerker

回答

1

(只是)这样做服务器端

$price_max = empty($_GET['price-max']) ? 999 : $_GET['price-max']; 
//          /\ default value 

这样一来,当您在查询中使用$price_max,这将是任何用户输入或默认值(999 - 无论你决定去的价值)。

你甚至不必乱搞网址来实现这一点。


上面的代码是一样的:

if(empty($_GET['price-max'])){ 
    $price_max = 999; // default value 
}else{ 
    $price_max = $_GET['price-max']; 
} 

图片的标题说明:

0

你只需要定义费尔德默认值

<input type="text" name="price-max" class="form-control" placeholder="Max Price" value="999"> 
+0

如果用户退格文本字段会发生什么情况。 –

+0

它仍然是空的值 – mollie

0

如果你想要把默认值,你应该在每个输入字段定义值。

<input type="text" value="defualt_value" name="price-max" class="form-control" placeholder="Max Price" > 
0

代替直接发送form的,调用一个函数来验证表单字段。在该函数中,检查该字段是否为空,然后添加默认值。最后,该函数提交form

0

1日法

定义value attributeinput field但这会显示您的default valuetext field

<form method = "GET" action="search.php"> 
<input type="text" name="price-max" class="form-control" placeholder="Max Price" value = "<?php echo $_GET['price-max'] ? $_GET['price-max'] : 999 ?>"> 
<input type="submit" value="submt"> 

如果你没有输入,则它将搭载price-max = 999否则无论你将键入

第二种方法

使用jQueryjavascript

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<form method = "GET" action="search.php" id="my_form"> 
<input type="text" name="price-max" id="price-max" class="form-control" placeholder="Max Price"> 
<input type="submit"> 
</form> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#my_form").submit(function() {   
      if($("#price-max").val()=="") {    
        $("#price-max").val('999');   
          } 
     }); 
    }); 
</script> 

我已经加入idattributeformprice-max

相关问题