Kubik Sukenik
Kubik Sukenik

Reputation: 49

Two search bars in one php page

I have two search bars in one php page, but second doesnt work. I have radio button group:

<form>
    <p><input type="radio" value="com" name="radioPM" />One</p>  
    <p><input type="radio" value="all" name="radioPM" />Two</p>  
</form>

with some jQuery, which showing div, depending on the radio selection:

<script>
    $(document).ready(function () {
        $('input[type=radio][name=radioPM]').change(function () {
            if (this.value == 'com') {
                $("#comDiv").show();
                $("#allDiv").hide();
            } else if (this.value == 'all') {
                $("#allDiv").show();
                $("#comDiv").hide();
            }
        });
    });
</script>

after that, I have two divs, which each of them has inside his own search bar:

<div id ='comDiv' style="display:none">

    <input type="text" id="myInput" onkeyup="searchByName()" placeholder="search..." title="search...">

    <?php
    include_once('query_1.php');

    echo "<table border='1' id='myTable'>
                    <tr>
                        <th>One</th>
                        <th>Two</th>
                    </tr>";

    while ($row = mysqli_fetch_array($query_1)) {
        echo "<form method=\"post\"><tr>";
        echo "<td>" . $row['ONE'] . "</td>";
        echo "<td>" . $row['TWO'] . "</td>";
        echo "</tr></form>";
    }
    echo "</table>";
    ?><br>

</div>

<div id ='allDiv' style="display:none">

    <input type="text" id="myInput" onkeyup="searchByName()" placeholder="search..." title="search...">

    <?php
    include_once('query_2.php');

    echo "<table border='1' id='myTable'>
                    <tr>
                        <th>One</th>
                        <th>Two</th>
                    </tr>";

    while ($row = mysqli_fetch_array($query_2)) {
        echo "<form method=\"post\"><tr>";
        echo "<td>" . $row['ONE'] . "</td>";
        echo "<td>" . $row['TWO'] . "</td>";
        echo "</tr></form>";
    }
    echo "</table>";
    ?><br>

</div>

and finally at the end i have script with function of search bar:

<script>
    function searchByName() {
        var input, filter, table, tr, td, i;
        input = document.getElementById("myInput");
        filter = input.value.toUpperCase();
        table = document.getElementById("myTable");
        tr = table.getElementsByTagName("tr");
        for (i = 0; i < tr.length; i++) {
            td = tr[i].getElementsByTagName("td")[1];
            if (td) {
                if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    }
</script>

My problem is, that if I select first option (value=com) everything works fine, comDiv appears, search bar searches in table from mysql DB. But if i choose second option (value=all),comDiv hides, and allDiv appears, data from mysql DB shows, but search bar not working. Can you please help me, what im doing wrong?

Upvotes: 1

Views: 1011

Answers (2)

suresh bambhaniya
suresh bambhaniya

Reputation: 1687

id must be unique within the HTML document(myInput and myTable used twice)

you can update your code like below or use conman search box

<input type="text" id="myInput1" onkeyup="searchByName('myInput1','myTable1')" placeholder="search..." title="search..."> // id as searchByName parameter 

<input type="text" id="myInput2" onkeyup="searchByName('myInput2','myTable2')" placeholder="search..." title="search...">

<script>
    function searchByName(myInput,myTable) {
        var input, filter, table, tr, td, i;
        input = document.getElementById(myInput);
        filter = input.value.toUpperCase();
        table = document.getElementById(myTable);
        tr = table.getElementsByTagName("tr");
        for (i = 0; i < tr.length; i++) {
            td = tr[i].getElementsByTagName("td")[1];
            if (td) {
                if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    }
</script>

and change table id echo "<table border='1' id='myTable1'> and "<table border='1' id='myTable2'>

Upvotes: 1

user10555852
user10555852

Reputation: 23

I think you should give specific selector in function searchByName(). Because both table have same id "myTable".Eeither you should change table ids or you can specify like $("#comDiv").find("myTable") and $("#allDiv").find("myTable").

Upvotes: 1

Related Questions