John
John

Reputation: 619

Building URL from input value using PHP

Site1: http://www.sitea.com

Site2: http://www.siteb.com

I would like to use the input field value from Site1 to build an url within a <a href=""> element that will point to Site2. This will be used to access search functionality of another site.

Example: input value = test-search

URL: http://www.siteb.com/#!q=test-search?parameter1

My attempt look like this:

    <form action="index.php" method="GET">
        <input type="search" name="search" />
    </form>
    <a href="https://www.siteb.com/#!q=<?php echo $_GET['search']; ?>?parameter1">Search</a>

I was looking through different tuttorials but could not find one that will use PHP to pass the value of the input field and build the URL.

Please note: I am limited to PHP only and no JS at all.

AMP example: - link to example

<form method="GET"
  class="p2"
  action="/components/amp-form/submit-form"
  target="_top">
  <div class="ampstart-input inline-block relative mb3">
    <input type="search"
      placeholder="Search..."
      name="googlesearch">
  </div>
  <input type="submit"
    value="OK"
    class="ampstart-btn caps">
</form>

Upvotes: 0

Views: 676

Answers (2)

Richard Muvirimi
Richard Muvirimi

Reputation: 844

in your main index.php file you will have this

<form method="GET"
      class="p2"
      action="submit-folder"
      target="_top">
    <div class="ampstart-input inline-block relative mb3">
        <input type="search"
               placeholder="Search..."
               name="googlesearch">
    </div>
    <input type="submit"
           value="OK"
           class="ampstart-btn caps">
</form>

and inside a folder named "submit-folder" with another index.php file inside yo will have this

  <?php
if (isset($_GET["googlesearch"])) {
    header("Location: https://www.siteb.com/#!q=" . $_GET["googlesearch"] . "?parameter1");
}

this is the file that will receive the request then automatically redirect you could have done this in a single file though as

<?php
if (isset($_GET["googlesearch"])) {
    header("Location: https://www.siteb.com/#!q=" . $_GET["googlesearch"] . "?parameter1");
    exit;
}
?>
<form method="GET"
      class="p2"
      action="<?php echo $_SERVER['PHP_SELF']; ?>"
      target="_top">
    <div class="ampstart-input inline-block relative mb3">
        <input type="search"
               placeholder="Search..."
               name="googlesearch">
    </div>
    <input type="submit"
           value="OK"
           class="ampstart-btn caps">
</form>

Upvotes: 1

Andrzej S.
Andrzej S.

Reputation: 472

You have to make redirect in you action script:

<?php
if (isset($_GET['search']))
    header("Location: https://www.siteb.com/#!q=".$_GET['search']."?parameter1"); 
?>

Upvotes: 1

Related Questions