1

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
6
  • define a value in the input field itself Commented Aug 9, 2016 at 10:06
  • 2
    Do that server-side, something like $var = empty($_GET['price-max']) ? 999 : $_GET['price-max']; Commented Aug 9, 2016 at 10:07
  • Is there JavaScript allowed? Commented Aug 9, 2016 at 10:11
  • Interestingly enough, the docs have an example like my previous comment: Example #3 Assigning a default value Commented Aug 9, 2016 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 Commented Aug 9, 2016 at 10:18

6 Answers 6

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:

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

You just need to define feild default value

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

2 Comments

what happen if user backspaces the text field.
it still empty value
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.

Comments

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

Comments

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', '');
    .......
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.