0

I'm trying to make a simple search for Wikipedia where I can type what I want to search and a select where I can select the language. What I'm trying to do is to get the select value to be able to search in different languages so I just replace the language string in the url of wikipedia.org

(e.g. If I select French in the select dropdown the form should redirects me to fr.wikipedia.org and if I select English, it should redirects me to en.wikipedia.org)

Here's what I tried so far:

<form action="https://<?php $_POST["language"] ?>.wikipedia.org/w/index.php">
  <input name="search" type="text" />
  <select name="language">
    <option value="en">English</option>
    <option value="fr">French</option>
  </select>
</form>

Now, on submit I get this url: https://.wikipedia.org/w/index.php?search=cat

How to pass select value to form action so I can add it at the start of the URL?

  • 1
    Can you confirm the current functionality by doing `var_dump($_POST);` and `var_dump($_GET);`? I'd expect your current URL to be https://.wikipedia.org/w/index.php?search=cat&language=en – WOUNDEDStevenJones Apr 30 '20 at 15:16

2 Answers2

1

JS will make it a lot easier than PHP.

EDIT: This is only for your specific usage. If you want a more general way to do this using PHP instead of javascript, look at the answer by @WOUNDEDStevenJones

<input id="srch" type="text" />
  <select id="lang" name="language">
    <option value="en">English</option>
    <option value="fr">French</option>
  </select>
  <button onclick="search()">Search!</button>

  <script>
      function search() {
          var term =document.getElementById("srch").value;
          var lang = document.getElementById("lang").value;
                    var link = "https://"+lang+".wikipedia.org/wiki/"+term;
                    location.replace(link);
      }
  </script>
1

The reason your current script isn't working is because PHP gets processed when the page is loaded, not after you submit the form. The first time you go to this page, $_POST['language'] isn't set, so the form action gets set to https://.wikipedia.org/w/index.php. Then if you submit the form (get) you'll end up at https://.wikipedia.org/w/index.php?search=cat&language=en.

Instead what you need to do is process the form submission on your page, and then redirect to Wikipedia. By default, when you submit a form (with a default method of get) you'll end up at a page like /index.php?search=cat&language=en. So now you can read in those $_GET parameters to construct the URL to redirect to.

<?php
    if (isset($_GET) && count($_GET)) {
        $language = $_GET["language"];
        $search = $_GET["search"];
        $action = "https://".$language.".wikipedia.org/w/index.php?search=".$search;
        header('Location: '.$action);
    }
?>

<form>
  <input name="search" type="text"/>
  <select name="language">
    <option value="en">English</option>
    <option value="fr">French</option>
  </select>
</form>

Another way would be to set <form method="post"> and then in PHP use $_POST like:

if ($_POST) {
    $language = $_POST["language"];
    $search = $_POST["search"];
    $action = "https://".$language.".wikipedia.org/w/index.php?search=".$search;
    header('Location: '.$action);
}
WOUNDEDStevenJones
  • 5,150
  • 6
  • 41
  • 53