This is a Simple Live Data Search of Mysql table using Jquery Ajax And PHP Mysqli.
- First Create A Database called "10am" in phpmyadmin.
- After Creating Database in phpmyadmin create table called 'lalita' .
- Then Add Column's on lalita as id and set as autoincrement ,Name,Address,City,Country and set as varchar.
- And last of database section insert some values on table columns
Now Create php file index.php
Copy this php scripts and paste it on index.php file .
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>bramento online study</title>
- <script src="js/jquery-1.10.2.min.js"></script>
- <script src="js/bootstrap.min.js"></script>
- <link href="css/bootstrap.min.css" rel="stylesheet" />
- </head>
- <body>
- <div class="container">
- <br />
- <h2 align="center"> Simple Live Data Search of Mysql table using Jquery Ajax And PHP Mysqli.</h2><br />
- <div class="form-group">
- <div class="input-group">
- <span class="input-group-addon">Search</span>
- <input type="text" name="search" id="search_text" placeholder="Write Something To Search" class="form-control" />
- </div>
- </div>
- <br />
- <div id="result"></div>
- //javascripts to call ajax
- <script>
- $(document).ready(function(){
- $('#search_text').keyup(function(){
- var txt = $(this).val();
- if(txt != '')
- {
- $.ajax({
- url:"fetch.php",
- method:"post",
- data:{search:txt},
- dataType:"text",
- success:function(data)
- {
- $('#result').html(data);
- }
- });
- }
- else
- {
- $('#result').html('');
- }
- });
- });
- </script>
- </div>
- </body>
- </html>
Now Create another php file called fetch.php
Copy and paste this php scripts on fetch.php file.
- <?php
- // Database Connection
- $connect = mysqli_connect("localhost", "root", "", "multi");
- $output = '';
- // Query for Select And Search
- $sql = "SELECT * FROM lalita WHERE Name LIKE '%".$_POST["search"]."%'";
- $result = mysqli_query($connect, $sql);
- if(mysqli_num_rows($result) > 0)
- {
- $output .= '<h3 align="center">Search Found</h3>';
- $output .= '<div class="table-responsive">
- <table class="table table bordered">
- <tr>
- <th>Name</th>
- <th>Address</th>
- <th>City</th>
- <th>Country</th>
- </tr>';
- while($row = mysqli_fetch_array($result))
- {
- $output .= '
- <tr>
- <td>'.$row["Name"].'</td>
- <td>'.$row["Address"].'</td>
- <td>'.$row["City"].'</td>
- <td>'.$row["Country"].'</td>
- </tr>
- ';
- }
- echo $output;
- }
- else
- {
- echo 'Data Does Not Found Here';
- }
- ?>
No comments:
Post a Comment