Receive input value and pass in URL

-2

I need to pass the selected value in a Bootstrap datepicker to the URL, but when I click to run, nothing happens:

<?php 
    if(isset($_GET['start_range']))
    {
        $start_range = $_GET['start_range'];
        $end_range = $_GET['end_range'];
    }

?>
<input  type="text" class="form-control" placeholder="Inicial"  id="start_range" name="start_range" value="<?php echo $start_range ?>">
<input  type="text" class="form-control" placeholder="Final"  id="end_range" name="end_range" value="<?php echo $end_range ?>"> 
<a href="?start_range=<?=$start_range?>&end_range=<?=$end_range?>" class="btn btn-primary btn-xs" role="button">Ir</a>
    
asked by anonymous 16.12.2014 / 21:23

1 answer

0

And there we go ... (Broom Witch, Woodpecker)

$ _GET and $ _POST are two superglobal arrays that are always available regardless of scope, that is, they will be available in and out of functions / class methods without you having to do anything to use them.

However, although always available, they will only have values stored under certain circumstances.

If the request is made via POST, manually by a form that has the term post defined in its action attribute:

<form action="" method="post">
    <input name="name" value="John" />
    <input name="location" value="Boston" />
    <input type="submit" value="Go!" />
</form>

Or via AJAX with posting data:

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})

$ _ POST will be populated. For examples of the code snippets above, debugging this variable with print_r () or var_dump () you would have:

array (2) {["name"] = > string (4) "John" ["location"] = > string (6) "Boston"}

$ _GET , in addition to the two scenarios above, but with attributes defined as GET instead of POST is also populated with the quesrystrings defined in the URL.

Querystrings , roughly are key = value pairs that are present in the URL at the time of its execution. And multiple pairs are separated by the & amp; ampersand character):

http://www.site.com/index.php?name=John&location=Boston

Now that everything has been explained, let's get to your problem.

You want the data you've trafficked via POST to appear in the URL. In the same Requisition, as far as I know, only with PHP is impossible because each Request is set up and executed in a different way.

But because of its code it seems to me that a result pagination is happening, it becomes possible because the first Requisition can travel via POST and subsequent ones by GET.

Assuming this is the scenario, your code looks like this:

if( isset( $_POST['start_range'] ) ) {

    $start_range = $_POST['start_range'];

} else {

    $start_range = ( isset( $_GET['start_range'] ) ? $_GET['start_range'] : 10 );
}

That is, if the Request is made by POST, the $ start_range variable will receive the value stored in $ _POST (if any).

If the Request is not made via POST, the else will be parsed and, if the parameter exists in the URL and consequently present in $ _GET, it will be used.

Finally, if all else fails, we set a default value to not break logic.

Wherever you go to use this variable, simply echo its value, either in a form field or in the href attribute of a link.

  

The same explanation above applies to the variable $ end_range

    
20.12.2014 / 20:57