将表单值传递给PHP搜索

时间:2016-07-28 22:41:21

标签: php html search

所以我正在尝试使用PHP和HTML构建基本的搜索引擎

现在我的API方面通过手动输入值进入查询

但我想创建表单,以便用户可以输入自己的值

我有表单,我只是想弄清楚如何传递API调用中的值

任何帮助都会很棒

所有重要代码都在

之下
<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>

3 个答案:

答案 0 :(得分:1)

下面是针对您的问题的经过全面测试和运行的解决方案。

<!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>

所以我的朋友在这里你有一些背景,如果你想制作100个搜索框,它也是动态的

答案 1 :(得分:0)

为您的表格 将输入设置为以下

<input type="text" name="search[]"/>

将其用于您要使用的每个搜索字段。

将表单操作修改为网址,或者将其留空以使其加载到同一页面上。

将method属性添加到表单字段并将其设置为“post”

在php部分,您可以通过阅读$ _POST [“搜索”]发送参数,它将作为数组返回,以便玩得开心。

我目前在手机上。如果你不太了解的话,会为你打字。

答案 2 :(得分:0)

因此,如果我理解正确 - 您希望用户能够通过表单输入搜索字词吗?

首先正确构建您的表单 - 您目前设置了action错误,我认为应该是方法。

<form method="POST" action="">
 Search Value 1: <input type="text" name="input_1" placeholder="Enter your first search term"><br>
 Search Value 2: <input type="text" name="input_2" placeholder="Enter yuor second search term"><br>
 <input type="submit" value="Submit">
</form>

现在您的表单很有意义,因此一旦用户按下提交按钮,他们在表单中输入的值将在POST请求中可用。提交表单只会刷新页面,因为action=""

现在您想通过执行

来检索这些值
  $searchTermOne = ""; //initailze the variables
  $searchTermTwo = "":
 if($_ISSET['input_1']){ //check if the value was set, it should be if the form was submitted
    $searchTermOne = $_POST['input_1']; //assign the value to the variable
 }
 if($_ISSET['input_2']){
    $searchTermTwo = $_POST['input_2'];
 }

然后您可以在查询参数中设置它们

$parameters = array(
    'api_key'   =>  "API_KEY_HERE",
     'query'   =>  array('Input1' => $searchTermOne, 'Input2' => $searchTermTwo)
);

我没有测试过这段代码,但它应该非常接近!

如果您需要任何澄清,请告诉我

相关问题