Receive input value and pass the URL

Asked

Viewed 1,958 times

-3

I need to pass the selected value on 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>
  • "when I click to execute, nothing happens:" - click where? at the anchor that is in the code? can put the rendered HTML also pf

  • 1

    Could be a problem with short tags. Try changing <?=$start_range?> by the standardized solution, which is <?php echo $start_range; ?> and <?php echo $end_range; ?>. But if what you want is to change the URL in real time, then it cannot be by PHP, you would have to use JS to update dynamically, because PHP runs before the page is sent.

  • @Expl0it Put more details in your question, can not know how you are trying to do this.

  • And your question is?

  • 1

    Expl0it in the issue put only the question, after voting to reopen you can add an answer.

  • As @Orion said, the question should stay as it was, and the answer posted below, in the answer area. Reverti editing (but you can see what had posted on historical). I’m also reopening the question so you can add the answer, okay?

Show 1 more comment

1 answer

0

And away we go... (Broomstick Witch, Woodpecker)

$_GET and $_POST are two superglobal arrays that are always available independent of scope, that is, they will be available in and out of class functions/methods without you having to do anything to be able 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 attribute action:

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

Or via AJAX with data posting:

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

$_POST will be filled. Examples of the above code fragments when debugging this variable with print_r() or var_dump() you would have:

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

Already $_GET, in addition to the above two scenarios, but with the 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 character & (ampersand):

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

Now that everything has been explained, we’ll get to your problem.

You want the data you have traffic via POST to appear in the URL. In the same Request, as far as I know, only with PHP is impossible because each Request is confided and executed in a different way.

However, as by your code it seems to me that there is a pagination of results, it becomes possible because the first request can traffic via POST and the subsequent by GET.

Assuming that’s the scenario, your code would look 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 made comes by POST, the variable $start_range 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 therefore present in $_GET, it will be used.

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

Wherever you are going to use this variable, simply echo its value, either in a form field, or in the attribute href one-link.

The same explanation above is valid for the variable $end_range

Browser other questions tagged

You are not signed in. Login or sign up in order to post.