Search code examples
phpgoogle-custom-search

Can I have multiple $_GET with the different key, same values?


I have search over the internet but did not get actual solution. What I found is "multiple $_GET with the same key, different values?" but I want to know that how can set different key with same value like ?t=helloWorld&?q=helloWorld, I know it is possible but I don't know the procedure to make this happen. Anyone can help me.?

So, My Question is I want to use www.omdbapi.com API and Google CSE together where omdb needs key '?t=' and Google CSE need '?q=' I have implemented both on www.moviesnight.club but it is not working because I can't duplicate the value of key '?t=' for the key '?q='.


Solution

  • I did not found any solution using only PHP so the JavaScript help me to make it done. Check the following snippets.

    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 (to print the submitted URL, you don't have to use this php code while using it, it is just for test purpose. It will make sure that code is working.)

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

    Also check the live PhpFiddle