Maintain value in inputs

1

I need your help, what happens is that when I enter the month and year in the inputs and I click on the filter button, the page reloads and the inputs are empty and I need them to stay there.

<?php

if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];
    $valueToSearch2 = $_POST['valueToSearch2'];
    // search in all table columns
    // using concat mysql function
    $query = "SELECT * FROM facturacion where item_mes  LIKE '$valueToSearch' AND item_anio LIKE '$valueToSearch2' ORDER BY 'item_dia' ASC";
    $search_result = filterTable($query);
}
 else {
    $query = "SELECT * FROM 'facturacion'";
    $search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{
    $connect = mysqli_connect("localhost", "root", "", "registrador");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
}

?>

<!DOCTYPE html>
<html>
    <head>
        <title>PHP HTML TABLE DATA SEARCH</title>
        <style>
            table,tr,th,td
            {
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        
        <form action="sss.php" method="post">
            <input type="text" name="valueToSearch" placeholder="Ingrese Mes"><br><br>
            <input type="text" name="valueToSearch2" placeholder="Ingrese Año"><br><br>
            <input type="submit" name="search" value="Filter"><br><br>
            
            <table>
                <tr>
                   
                    <th>Sujetas</th>
                    <th>Agravadas</th>               
                    <th>Total</th>
                    
                </tr>

      <!-- populate table from mysql database -->
                <?php 
                while($row = mysqli_fetch_array($search_result)):?>
                <tr>
                    
                    <td><?php echo $row['item_sujetas'];?></td>
                    <td><?php echo $row['item_gravadas'];?></td>
                    <td><?php echo $row['item_total'];?></td>
                </tr>

                <?php endwhile;?>
            </table>
        </form>
        
    </body>
</html>
    
asked by outsider 10.09.2018 в 05:52
source

1 answer

3

You have to add the attribute "value" to the input of month and year with the corresponding value:

<input type="text" name="valueToSearch" placeholder="Ingrese Mes" value="<?php echo array_key_exists('valueToSearch', $_POST) ? $_POST['valueToSearch'] : ''; ?>"><br><br>
<input type="text" name="valueToSearch2" placeholder="Ingrese Año" value="<?php echo array_key_exists('valueToSearch2', $_POST) ? $_POST['valueToSearch2'] : ''; ?>"><br><br>
    
answered by 10.09.2018 / 06:04
source