0

here is my code for the ajax and php together. I Need to fetch all the table data when page is loaded, i try many thing but could't get any proper solution. i want to use only html table to fetch all the data in it.You can also modify select statement for all the data now it is only running for some specific id which is selected by user.

<html>
<head>
    <script>
    function showUser(str) {
        if (str == "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else {
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("txtHint").innerHTML = this.responseText;
                }
            };
            xmlhttp.open("GET", "getuser.php?q=" + str, true);
            xmlhttp.send();
        }
    }
    </script>
</head>

<body>
    <form>
        <select name="users" onchange="showUser(this.value)">
            <option value="">Select a person:</option>
            <option value="1">user1</option>
            <option value="2">user2</option>
            <option value="3">user3</option>
            <option value="4">user4</option>
        </select>
    </form>
    <br>
    <div id="txtHint"><b>Person info will be listed here...</b></div>
</body>

</html>
<!DOCTYPE html>
<html>

<head>
    <style>
    table {
        width: 100%;
        border-collapse: collapse;
    }

    table,
    td,
    th {
        border: 1px solid black;
        padding: 5px;
    }

    th {
        text-align: left;
    }
    </style>
</head>

<body>
    <?php
        $q = intval($_GET['q']);// gting userid 

        $con = mysqli_connect('localhost','root','');
        if (!$con) {
            die('Could not connect: ' . mysqli_error($con));
        }

        mysqli_select_db($con,"tabledata"); //table  name
        $sql="SELECT * FROM user_info  WHERE id = '".$q."'"; // query
        $result = mysqli_query($con,$sql);

        echo "<table>
        <tr>
        <th>Id</th>
        <th>name</th>
        <th>email</th>
        </tr>";
        while($row = mysqli_fetch_array($result)) //loop to fetch data {
            echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['name'] . "</td>";
            echo "<td>" . $row['email'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        mysqli_close($con);
    ?>
</body>
</html>
Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56

0 Answers0