【问题标题】:Can I have multiple $_GET with the different key, same values?我可以有多个具有不同键、相同值的 $_GET 吗?
【发布时间】:2015-11-13 05:09:09
【问题描述】:

我在互联网上进行了搜索,但没有得到实际的解决方案。我发现的是“multiple $_GET with the same key, different values?” 但我想知道如何设置具有相同值的不同键,如 ?t=helloWorld&?q=helloWorld ,我知道这是可能的,但我不知道实现这一点的程序。谁能帮帮我?

所以,我的问题是 我想在 omdb 需要密钥 '?t=' 和 Google CSE 需要 '?q=' 的情况下一起使用 www.omdbapi.com API 和 Google CSE 我已经在 www.moviesnight.club 上实现了两者,但它不起作用,因为我无法复制键 '?q=' 的键 '?t=' 的值。

【问题讨论】:

  • 是的,您可以设置多个具有相同值的 GET 参数。问题是什么?
  • header("Location: index.php?t=helloWord&q=helloWord"); 甚至是带有GET 方法的表单。
  • @YeldarKurmangaliyev 我想一起使用 www.omdbapi.com API 和 Google CSE,其中 omdb 需要密钥 '?t=' 而 Google CSE 需要 '?q=' 我在 www.movi​​esnight 上都实现了.club 但它不起作用

标签: php google-custom-search


【解决方案1】:

解决方案 1:

您应该将参数的值存储在变量中,然后检查$_GET 中的可用键:

$queryValue = '';

// Fetch parameter for omdbapi
if(isset($_GET['t'])) {
    $queryValue = $_GET['t'];
}

// Fetch parameter for Google CSE
if(isset($_GET['g'])) {
    $queryValue = $_GET['g'];
}

// Optional:
if(empty($queryValue)) {
    die('No parameter given.');
}

// Use the value stored in $queryValue

解决方案 2:(如果您无法使用 $queryValue 之类的变量)

$_GET['g'] = isset($_GET['g']) ? : $_GET['t'];
$_GET['t'] = isset($_GET['t']) ? : $_GET['g'];

【讨论】:

【解决方案2】:

我没有找到任何仅使用 PHP 的解决方案,因此 JavaScript 帮助我完成了它。检查以下 sn-ps。

JavaScript

function changeText(containerId) {
    var datatext = document.getElementById('omdbBox').value;
    var collection = document.getElementById(containerId).getElementsByTagName('INPUT');
    for (var x = 0; x < collection.length; x++) {
        if (collection[x].type.toUpperCase() == 'TEXT') collection[x].value = datatext;
    }
}

CSS

#cseBox {display: none;}

PHP(打印提交的URL,使用时不必使用此php代码,仅用于测试目的。它将确保代码正常工作。)

<?php
if(isset($_GET['search'])){
    echo 'http://moviesnight.club/?<b>t='.$_GET['t'].'&q='.$_GET['q'].'</b>&search=';
}
?>

HTML

<form method="get">
    <input id="omdbBox" type="text" autocomplete="off" name="t" onkeyup="changeText('cseBox')">
    <div id="cseBox">
        <input type="text" name="q">
    </div>
    <input type="submit" name="search">
</form>

还可以查看直播PhpFiddle

【讨论】:

    猜你喜欢
    • 2012-05-27
    • 2014-04-27
    • 1970-01-01
    • 2017-06-24
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 2011-10-12
    相关资源
    最近更新 更多