0

i need help with append row/s to table selected from database.

i have index.php with form:

    <form action="" id="pos_data" method="post">
                <input type="text" name="typeSearch" placeholder="Enter Type Number to Search" /><br/><!-- comment -->
                <input type="submit" name="searchbtn" value="Search Data">
    </form>
 <?php
    require_once 'db.php';
    require_once 'load_table.php';
          
        
            
    echo'<div id="refresh_table"></div><br/>';
?>

and i have load_table.php (show data from database to table):

<?php
        include 'db.php';  //other file with conn to database
        
                $query = "Select....."; //10 rows 
                           
                $result = sqlsrv_query($dbhandle, $query)
                        or die('A error occured: ' . mysql_error()); //$dbhandle define in db.php
                        
                 $o = '<table id="myTable">
                        <thead>
                        <tr>
                        <th>TYPE </th>
                        <th>Line</th>
                        <th>Free Space </th>
                        <tbody></tbody>
                        </tr>
                        </thead><tbody>';
                
                while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
                    
                       $o .= '<tr><td>'.$row ['MATNR'].'</td> <td>'.$row ['NLPLA'].'</td> <td>'.$row ['freeSpace'].'</td> </tr>';
             
                }
                $o .= '</tbody></table>';
            echo $o;

         sqlsrv_free_stmt($result);
         sqlsrv_close($dbhandle);
            ?>

It works well. The table appears and next to the table there is a bar with a button.

And I would like when I write something in the search field, it will search for it again in the DB (according to the given query) and add a row / rows to the original table. He tried this in another file search_load.php:

<?php
 include 'db.php';
 include 'load_table.php';
    if(isset($_POST['searchbtn']))
                {
                    $typeNumber = $_POST['typeSearch'];
                    $querySearch = "Select .....  WHERE MATNR LIKE '%".$typeNumber."%'";
                    $resultSearch = sqlsrv_query($dbhandle, $querySearch)

But I don't know how to attach it to the original table. Actually, I don't know if it can work like this. If not, how could it work?

Cesc
  • 274
  • 2
  • 14
  • Am I correct that you want to display the original table rows as well as the search result rows? Not only the search result? – Michel Jun 10 '21 at 08:13
  • @Michel Yes, exactly. – Cesc Jun 10 '21 at 08:18
  • 1
    Then you have two options: 1) reload the whole page and include the search results in the original table 2) use [AJAX](https://stackoverflow.com/q/1510011/1685196) to load only the search results and append them to the origibal table. – Michel Jun 10 '21 at 08:26
  • @Michel Thank you very much for the clarification. Important for me to know. How will appending a line work using AJAX? Because using ajax, I regularly update the original table at regular intervals. – Cesc Jun 10 '21 at 08:30
  • Retrieve the data trough ajax and with javascript something like `document.getElementById("table").innerHTML + = data` – Michel Jun 11 '21 at 14:09

0 Answers0