1

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?

7
  • Maybe use sessions or cookies for the first request, you can only do one request per request. Commented Feb 13, 2017 at 14:45
  • Why have two forms if you want them to be submitted at the same time with data from both? Commented Feb 13, 2017 at 14:49
  • Why use different forms? Commented Feb 13, 2017 at 14:50
  • @MagnusEriksson The Problem is that i have a search field which is the 'Form 1' in my description. This search field is on every page on my website. If I'm submitting this form, it sends the data to search.php where i can choose between two options, 'movies' and 'series'. This two options are my second form. Commented Feb 13, 2017 at 14:52
  • @C0dekid Thats not what im trying to do, both parameters have to be in the URL... Anyways, thank you for your answer Commented Feb 13, 2017 at 14:57

2 Answers 2

1

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.

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

Comments

0

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

1 Comment

You can also do the same without including a bloated JS framework, unless you're already using it for other stuff.

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.