繁体   English   中英

如何将 2 个搜索 forms 合并在一起?

[英]How can I merge 2 search forms together?

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

第一个搜索表单搜索产品第一个搜索表单第二个搜索产品的位置第二个搜索表单

很抱歉问了这个愚蠢的问题,谢谢你的帮助:D

Javascript 是您开始学习所需要的。 它是网页设计必不可少的另一半。 使用 Javascript,您可以从页面上的任何内容中获取价值。 这是从两个不同 forms 的输入中提取值并将它们发布到 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>

你的PHP文件.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);

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM