【问题标题】:How can I merge 2 search forms together?如何将 2 个搜索表单合并在一起?
【发布时间】:2020-06-08 22:02:54
【问题描述】:

我在 Wordpress 上做 my website。 我的网站上有 2 个单独显示的搜索表单,我想知道如何将它们合并为一个。

第一个搜索表单搜索产品First search form 第二个搜索产品的位置Second search form

很抱歉提出这个愚蠢的问题,感谢您的帮助:D

【问题讨论】:

  • “将它们合并为一个”是否意味着您要使用输入到它们两者中的值来执行搜索?喜欢他们正在搜索的内容,并且在他们所在位置的距离内?
  • 欢迎来到 Stack Overflow。你已经尝试过什么来做到这一点?请查看How do I ask a good question。这个Question Checklist 看看我们需要什么。准备好后,您可以在minimal, complete, and reproducible example 中使用您尝试过的详细信息和相关代码更新您的问题,以便我们提供帮助。
  • 你好亚当温特。是的,这就是我的意思。基本上是一个将产品和位置放在一起的搜索表单。
  • 我曾尝试在 google chrome 检查上的 html 上编写代码,但我不知道 php,如果我没有错的话,那就是那里使用的语言。感谢您尝试帮助我将更新我的问题。

标签: php wordpress search


【解决方案1】:

Javascript 是您开始学习所需的。它是网页设计必不可少的另一半。使用 Javascript,您可以从页面上的任何内容中获取价值。这是从两种不同形式的输入中提取值并将它们发布到您的 PHP 文件的基本代码。

<!--first form-->
<form>
    <input type="text" id="product" name="product">
</form>

<!--second form-->
<form>
    <input type="text" id="city" name="city">
</form>

<div id="result"></div>

<script>
function continuePost() {

    var product = encodeURIComponent(document.getElementById("product").value);
    var city = encodeURIComponent(document.getElementById("city").value);
    var params = "product="+product+"&city="+city;

  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {

        var myResponse = JSON.parse(this.responseText);  //send a JSON response back from your PHP file

        if(myResponse.hasOwnProperty('error')){
            document.getElementById("result").innerHTML = myResponse.error;
        }else{
            var result1 = myResponse.myResult1;
            var result2 = myResponse.myResult2[0];   //or whatever key and value pairs that you used in the JSON response that you sent back from you PHP file
            document.getElementById("result").innerHTML = result1;
        }

    }else{
      window.setTimeout(failed(), 3000);
    }
  };
  xhttp.open("POST", "yourPHPfile.php", true);
  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.send(params);
}

function failed(){
      document.getElementById("result").innerHTML = 'Failed to connect to server.';
}
</script>

你的PHPfile.php

<?php

$obj = new stdClass();
     $obj->dateread = date("D M j G:i:s T Y");

if(!isset($_POST["product"])){
    $obj->error = 'No product.';
    echo json_encode($obj);
    exit;
}else {
    $product=$_POST["product"];
    }

if(!isset($_POST["city"])){
    $obj->error = 'No city.';
    echo json_encode($obj);
    exit;
}else {
    $city=$_POST["city"];
    }

$obj->myResult1 = 'Info you want back from your PHP file.';
$obj->myResult2 = ['array', 'of', 'info', 'you', 'want'];

echo json_encode($obj);

?>

【讨论】:

  • 非常感谢这给了我一些指导!惊人的评论!谢谢你,谢谢你,谢谢你!!!!!!
  • 我添加了一个可用于页面的 PHP 文件的简单示例。不客气。
  • 也谢谢你,我可以找到我的代码来自哪里!我很惊讶我的 php 代码在 js 文件而不是 php 中!谢谢您的帮助。我会在接下来的几天里尝试这样做。
猜你喜欢
  • 1970-01-01
  • 2016-12-02
  • 2013-11-16
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2018-02-15
相关资源
最近更新 更多