0

I have a search form with various field but i want to set a default into the url if the field is empty:

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

and when the form is submited my url looks something like this

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

instead i want a default value so that when i submit the form with an empty price field the url should like.

search.php?location=location&category=Category&status=lease&price-max=999
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
Kofi Amoussou
  • 82
  • 1
  • 1
  • 10
  • define a value in the input field itself –  Aug 09 '16 at 10:06
  • 2
    Do that server-side, something like `$var = empty($_GET['price-max']) ? 999 : $_GET['price-max'];` – FirstOne Aug 09 '16 at 10:07
  • Is there JavaScript allowed? – yunzen Aug 09 '16 at 10:11
  • Interestingly enough, the docs have an example like my previous comment: [Example #3 Assigning a default value](http://us2.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) – FirstOne Aug 09 '16 at 10:18
  • yes js is allowed as long as it works , because i'm using it to query a phpmyadmin database, i should be able to insert changed GEt value into my query – Kofi Amoussou Aug 09 '16 at 10:18
  • So, basically, it doesn't really matter the url? You just want the default value if the input is empty? If so, my previous comments (that you seemed to overlook) are what you are looking for – FirstOne Aug 09 '16 at 10:20

6 Answers6

4

(Just) Do it server-side:

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

That way, when you use $price_max in your query, it will be either user input or the default value (999 - or whatever value you decide to go with).

You don't even have to mess with the url to achieve that.


The previous code is the same as:

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

Sidenotes:

Community
  • 1
  • 1
FirstOne
  • 6,033
  • 7
  • 26
  • 45
0

You just need to define feild default value

<input type="text" name="price-max" class="form-control" placeholder="Max Price" value="999">
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
mollie
  • 140
  • 1
  • 16
0

If you want to put default value, you should define value in each input field.

<input type="text" value="defualt_value" name="price-max" class="form-control" placeholder="Max Price" >
Gulmuhammad Akbari
  • 1,986
  • 2
  • 13
  • 28
0

Instead of sending the form directly, call a function to validate the form fields. In that function, check if that field is empty and then add your default value. Finally that function submits the form.

0

1st method

Define value attribute in your input field but this will show your default value in text 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">

if you didn't input then it will carry price-max = 999 otherwise whatever you will type

2nd Method

Use jQuery or javascript for this

<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>

I have added id attribute to form and price-max field

Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
0

in controller example

public function getMessages(Request $request){
    $page = request('page', 1);
    $limit = request('limit', 100);
    $status = request('status', 'all');
    $sort = request('sort', 'asc');
    $id = request('id', 'asc');
    $referenceId = request('referenceId', '');
    $from = request('from', '');
    $to = request('to', '');
    $ack = request('ack', '');
    .......
}
Waad Mawlood
  • 727
  • 6
  • 10
  • 1
    This answer is missing its educational explanation. In controller where these methods actually exist? Is this laravel? Please [edit] your question with the intent to educate/empower thousands of future researchers. – mickmackusa Jul 17 '22 at 12:42