TeemoBiceps
TeemoBiceps

Reputation: 496

PHP get request with multiple parameters from different forms

I'm new to PHP and have a little problem. I'm trying to set up a get Request with two parameters. The URL should for example look like this:

search.php?query=Harry+Potter&page=movies

The problem is, that i get the two parameters 'query' and 'page' from two different forms.

My Code is the following:

Form 1:

<form action="search.php" method="get" class="navbar-form" role="search">
  <div class="input-group">
    <input type="text" class="form-control" placeholder="Search" name="query">
    <div class="input-group-btn">
      <button class="btn btn-default c-nav-btn-height" type="submit"><i class="glyphicon glyphicon-search"></i></button>
    </div>
  </div>
</form>

Form 2:

<form class="btn-group-vertical" action="search.php" method="get">
  <button id="bMovies" class="btn active-menu" type="submit" name="page" value="movies">Filme</button>
  <button id="bSeries" class="btn" type="submit" name="page" value="series">Serien</button>
</form>

Are there any possibilities to combine two forms in one get Request?

Upvotes: 1

Views: 3992

Answers (2)

TeemoBiceps
TeemoBiceps

Reputation: 496

I as able to fix the problem with some help from the comment section - thanks to @Magnus Eriksson. Unfortunately he deleted some of his comments, so I will answer this questions by myself.

To clarify, I know this is for sure not the best way to do it, but Hell Yeah, it works!

Solution:

Hidden input fields did it for me. I added one hidden input field in each form:

Form 1:

<form action="search.php" method="get" class="navbar-form" role="search">
  <div class="input-group">
    <input type="hidden" name="page" value="movies">
    <input type="text" class="form-control" placeholder="Search" name="query">
    <div class="input-group-btn">
      <button class="btn btn-default c-nav-btn-height" type="submit"><i class="glyphicon glyphicon-search"></i></button>
    </div>
  </div>
</form>

Form 2:

<form class="btn-group-vertical" action="search.php" method="get">
  <button id="bMovies" class="btn active-menu" type="submit" name="page" value="movies">Filme</button>
  <button id="bSeries" class="btn" type="submit" name="page" value="series">Serien</button>
  <input id="hiddenField" type="hidden" name="query" value="">
</form>

So I added a hidden input field named "page" in the first form and a hidden input field named "query" in the second form. With this little trick im able to get the same information in both forms, but only the fields I want to show are shown.

Since the Form 1 is submitted first, it is possible to copy the value of the visible input field into the invisible input field in Form 2. All i need is some extra PHP and JavaScript.

$query = $_GET["query"];
$page = $_GET["page"];
if ($page == "movies" || $page == "series") {
  echo "<script>";
  echo 'document.getElementById("hiddenField").value = "'. $query .'" ;';
  echo "</script>";
}
else {
}

I store the two parameters from the first Form into variables and check if $page is 'movies' or 'series', because only these two options are allowed on this site. If it is true, it echos some JavaScript, which is adding the $query variable as the value of the hidden input field in the second form.

And again, i know there will be better ways to do it, but it worked for me and i wanted to share it with you.

Upvotes: 1

Ralph Thomas Hopper
Ralph Thomas Hopper

Reputation: 743

well, not natively, and if you can avoid it, you should. if you can combine both forms on your page, do that. if.. for some reason you really can't, i suggest you have a look at this thread.. it has a javascript function to do what you are looking to accomplish jQuery - submit multiple forms through single reqest, without Ajax

Upvotes: 0

Related Questions