【问题标题】:Passing Form Values to PHP Search将表单值传递给 PHP 搜索
【发布时间】:2016-07-28 22:41:21
【问题描述】:

所以我正在尝试构建一个基本的搜索引擎,使用 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>

【问题讨论】:

    标签: php html search


    【解决方案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 个搜索框

    【讨论】:

    • 我在这里使用 POST,因为它比 GET 更适合发送数据,因为 GET 对它可以处理的数据大小有限制。
    【解决方案2】:

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

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

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

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

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

    在 php 部分中,您可以通过读取 $_POST["search"] 发送参数,它将作为数组返回,所以玩得开心。

    我目前正在使用手机。如果您有点不太明白,会为您输入一些内容。

    【讨论】:

    • 如果你有时间的话,更多的上下文会很棒
    • @ClarkPamler93 请查看我的新回复帖子以及详细信息,如果您需要任何帮助,请随时给我发电子邮件,或在任何 cmets 中标记我。
    【解决方案3】:

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

    首先要正确构建您的表单 - 您目前有 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)
    );
    

    我没有测试过这段代码,但应该很接近了!

    如果您需要任何说明,请告诉我

    【讨论】:

    • 是的,所以用户在搜索字段中输入 2 个值并单击提交,然后将这 2 个值传递到相应的 php,我也用正确的方法而不是操作更新了我的表单
    • @ClarkPamler93 对不起,我不小心提交了答案,现在看完整版
    猜你喜欢
    • 1970-01-01
    • 2019-04-10
    • 2015-08-31
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2012-05-14
    相关资源
    最近更新 更多