How to display the date in the input after the query

1

I have the following form:

<?php
include "conexao.php";
    echo"<form class='navbar-form navbar-left' action='#'>";
      echo"DATA INICIAL:&nbsp; &nbsp; <input type='text' name='datainicio' id='datainicio' class='form-control'>";
      echo"DATA FINAL :&nbsp; &nbsp; <input type='text' name='datafinal' id='datafinal'class='form-control' >";
      echo"OCORRENCIA :&nbsp; &nbsp; <select class='form-control' id='ocorrencia' name='ocorrencia'>";

        $result= mysql_query ("SELECT * FROM  ocorrencias") or die ("não foi possivel fazer a pesquina no banco");
        while ($row       = mysql_fetch_assoc($result))
        {
        $v_oc_id   = $row["oc_id"];
        $v_oc_desc = $row["oc_desc"];
              echo"<option value='$v_oc_id'>$v_oc_desc</option>";        
            }
        echo"</select>";
        echo"&nbsp; &nbsp; <button type='submit'class='btn btn-primary'><span class='  glyphicon glyphicon-search'></span></button>";
echo"</form>";

 ?>

What I need, after the query that the date chosen is in the date field, as shown in the image, ie after the query the date is still selected.

    
asked by anonymous 07.12.2016 / 18:46

3 answers

1

As your goal and submit the dates after the query you can retrieve this data by the variable post as shown below. If you have doubts about a var_dump ($ _ POST) and you will see all the post you made

<?php include "conexao.php"; ?>
        <form class='navbar-form navbar-left' action='#'>
            DATA INICIAL:&nbsp; &nbsp; <input type='text' name='datainicio' id='datainicio' value="<?php echo $_POST['datainicio']?>" class='form-control'>
            DATA FINAL :&nbsp; &nbsp; <input type='text' name='datafinal' id='datafinal' <?php echo $_POST['datafinal']?>class='form-control'>

            OCORRENCIA :&nbsp; &nbsp; <select class='form-control' id='ocorrencia' name='ocorrencia'>

                <?php
                $result = mysql_query("SELECT * FROM  ocorrencias") or die("não foi possivel fazer a pesquina no banco");
                while ($row    = mysql_fetch_assoc($result))
                {
                    $v_oc_id   = $row["oc_id"];
                    $v_oc_desc = $row["oc_desc"];
                    echo"<option value='$v_oc_id'>$v_oc_desc</option>";
                }
                ?>
            </select>
            <button type='submit'class='btn btn-primary'><span class='  glyphicon glyphicon-search'></span></button>
        </form>

        ?>
    
07.12.2016 / 19:03
0
<?php
include "conexao.php";

$datainicio='';
if(isset($_POST["datainicio"])){ $datainicio = $_POST["datainicio"]; }

$datafinal='';
if(isset($_POST["datafinal"])){ $datafinal = $_POST["datafinal"]; }

    echo"<form class='navbar-form navbar-left' action='#'>";
      echo"DATA INICIAL:&nbsp; &nbsp; <input type='text' name='datainicio' value='$datainicio' id='datainicio' class='form-control'>";
      echo"DATA FINAL :&nbsp; &nbsp; <input type='text' name='datafinal' value='$datafinal' id='datafinal'class='form-control' >";
      echo"OCORRENCIA :&nbsp; &nbsp; <select class='form-control' id='ocorrencia' name='ocorrencia'>";

            $v_oc_desc = 'v_oc_desc';
            echo"<option value='$v_oc_id'>$v_oc_desc</option>";        

        echo"</select>";
        echo"&nbsp; &nbsp; <button type='submit'class='btn btn-primary'><span class='  glyphicon glyphicon-search'></span></button>";
echo"</form>";

 ?>
    
07.12.2016 / 19:08
0

You can retrieve the data in the following way, as already mentioned in previous answers, through the global variable $_POST .

Regarding the error mentioned in the comments in the previous answer:

<br/><b>Notice</b>: Undefined index: datafinal in<b>/var/www/html/sistema/principal.php</b> on line <b>134</b><br/>, "como se inicia sem data retorna isso, e mesmo após a consulta continua retornando isso"

In order to avoid this, you can first define the variables of type string with a blank space 'espaço' , note not enough simple single quotes space is needed.

<?php
include "conexao.php";

$datainicio=' ';
if(isset($_POST["datainicio"])){$datainicio = $_POST["datainicio"];}
$datafinal=' ';if(isset($_POST["datafinal"])){$datafinal = $_POST["datafinal"];} 
    echo"<form class='navbar-form navbar-left' action='#' method='post'>";
    echo"DATA INICIAL:&nbsp; &nbsp; <input type='text' name='datainicio' id='datainicio' value='".$datainicio."' class='form-control'>";
    echo"DATA FINAL :&nbsp; &nbsp; <input type='text' name='datafinal' id='datafinal' value='".$datafinal."' class='form-control'>";
    echo"OCORRENCIA :&nbsp; &nbsp; <select class='form-control' id='ocorrencia' name='ocorrencia'>";

    $result= mysql_query ("SELECT * FROM  ocorrencias") or die ("não foi possivel fazer a pesquina no banco");
    while($row = mysql_fetch_assoc($result))
    {
        $v_oc_id   = $row["oc_id"];
        $v_oc_desc = $row["oc_desc"];
        echo"<option value='$v_oc_id'>$v_oc_desc</option>";        
    }

    echo"</select>";
    echo"<button type='submit'class='btn btn-primary'><span class='glyphicon glyphicon-search'></span></button>";
    echo"</form>";
?>

Take a little time, with this question: #

    
07.12.2016 / 22:40