Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将表单值传递给PHP搜索_Php_Html_Search - Fatal编程技术网

将表单值传递给PHP搜索

将表单值传递给PHP搜索,php,html,search,Php,Html,Search,因此,我正在尝试使用PHP和HTML构建一个基本的搜索引擎 现在,我让它的API端通过手动将值输入到查询中来工作 但是我想创建一个表单,这样用户就可以输入自己的值 我已经制作了表单,我只是不知道如何在API调用中传递值 任何帮助都会很好 所有相关代码如下 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; char

因此,我正在尝试使用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>

PHPFIDLE初始代码
搜索值1:用于您的表单
将输入设置为以下值:

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

对于您要使用的每个搜索字段都使用它

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

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

在php部分,您可以通过阅读$_POST[“search”]发送参数,它将作为数组返回,因此您可以尽情享受

我现在正在打电话。如果你一点也不懂,我会帮你打一些东西。

你的表格 将输入设置为以下值:

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

对于您要使用的每个搜索字段都使用它

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

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

在php部分,您可以通过阅读$_POST[“search”]发送参数,它将作为数组返回,因此您可以尽情享受


我现在正在打电话。如果您一点也不太懂,我会为您键入一些内容。

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

首先,正确地构建表单-您当前的
操作设置错误,我认为这应该是一种方法

<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>
然后可以在查询参数中设置它们

$parameters = array(
    'api_key'   =>  "API_KEY_HERE",
     'query'   =>  array('Input1' => $searchTermOne, 'Input2' => $searchTermTwo)
);
我还没有测试过这段代码,但应该非常接近


如果您需要任何澄清,请告诉我。如果我理解正确,您希望用户能够通过表单输入搜索词吗

首先,正确地构建表单-您当前的
操作设置错误,我认为这应该是一种方法

<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>
然后可以在查询参数中设置它们

$parameters = array(
    'api_key'   =>  "API_KEY_HERE",
     'query'   =>  array('Input1' => $searchTermOne, 'Input2' => $searchTermTwo)
);
我还没有测试过这段代码,但应该非常接近


如果您需要任何澄清,请告诉我。以下是针对您的问题的经过充分测试的有效解决方案

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

基本搜索引擎
搜索值1:
搜索值2:

因此,我的朋友,这里您有一些背景,它也是动态的,如果您希望创建100个搜索框,下面是一个经过充分测试的有效解决方案

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

基本搜索引擎
搜索值1:
搜索值2:

因此,我的朋友,这里你有一些上下文,它也是动态的,如果你想创建100个搜索框

当你有time@ClarkPamler93请查看我的新回复帖子,里面有详细的信息,如果你需要任何帮助,请随时给我发电子邮件,或者在任何评论中给我贴标签。多一些上下文会很好,当你有time@ClarkPamler93请查看我的新回复帖子,其中包含详细信息,如果您需要任何帮助,请随时向我发送电子邮件,或在任何评论中标记我。是的,因此用户在搜索字段中输入2个值,然后单击“提交”,这2个值将传递到相应的php,另外,我已经用正确的方法更新了我的表格,但没有action@ClarkPamler93抱歉,我无意中提交了答案,请查看完整版本是的,因此用户在搜索字段中输入2个值,然后单击“提交”,这2个值将传递到相应的php,另外,我已经用正确的方法更新了我的表格,但没有action@ClarkPamler93对不起,我无意中提交了答案,现在查看完整版本我在这里使用POST比GET更适合发送数据,因为GET在处理数据大小方面有限制。我在这里使用POST比GET更适合发送数据,因为GET在处理数据大小方面有限制。