ClarkPamler93 ClarkPamler93 - 4 months ago 8
HTML Question

Passing Form Values to PHP Search

So I am trying to build a basic search engine, using PHP and HTML

Right Now I have the API side of it working by manually putting in values, into the query

But I want to create a form so the user can enter there own values

I have the form made, I just cant figure out how to pass the values in the API call

Any help would be great

All revelant code is below

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>PhpFiddle Initial Code</title>

<script type="text/javascript">

</script>

<style type="text/css">

</style>

</head>

<body>
<form method="get">
Search Value 1: <input type="text" name="Input Value 1""><br>
Search Value 2: <input type="text" name="Input Value 2"><br>
<input type="submit" value="Submit">
</form>


<?php

$parameters = array(
'api_key' => "API_KEY_HERE"
, 'query' => array('Input Value 1' => 'Input Value 2')
);


?>

</body>
</html>

Answer

here below is a fully tested and working solution for your problem.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Basic Search Engine</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <!--
        Created By: Dean Van Greunen.
             Email: deanvg9000@gmail.com

        Form Attributes (Basic)
        - Action : This refers to where the data will go once the form is submited,
                   You may use a URL action="https://www.example.com/search"
                   a local URL such as action="/search"
                   or it may be left blank for the page to send the data back to itself. action=""

        - Method : this is how the data will be sent, you have 2 options
                   GET - This will send the form variables in the url and can be accessed via $_GET variable
                   POST - This will send it in the data block, which can be accessed via $_POST variable

    -->
    <form action="" method="POST">
        Search Value 1: <input type="text" name="search[]"><br>
        Search Value 2: <input type="text" name="search[]"><br>
        <input type="submit">
    </form>

    <?php
        //checks if anything has being posted to this page through a POST request.
        //on a normal page load a GET request is sent
        if(!empty($_POST)){
            //checks if any data is in the search variable
            if(!empty($_POST["search"])){
                //loops through all data and prints it
                for ($i=0; $i < count($_POST["search"]); $i++) { 
                    //checks if that search value in the array is empty or not
                    if(!empty($_POST["search"][$i])){
                        echo "Search ".($i+1)." is: ".$_POST["search"][$i]."<br>";
                    }
                }
            }

        }

        //the above just reprints the search so you know that it is work, a proof.
        if(!empty($_POST)){
          if(!empty($_POST["search"])){
                $parameters = array(
                    "api_key" => "API_KEY_HERE",
                    "query"   => $_POST["search"] //here am putting the request array of searches into the query.
                );
            }
        }
    ?>
</body>
</html>

So my friend here you have some context, it is dynamic as well, if you ever wish to make a 100 search boxes

Comments