How to display the date in the input after the query

Asked

Viewed 191 times

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 chosen date stays in the field date, as shown in the image, that is after the query the date remains selected. inserir a descrição da imagem aqui

  • friend vc can treat the form as html itself and not as echo php, how much the question vc can use the input value so the value coming back from the database appears as you want example:

  • vc can use input value so the value coming back from the bank appears as you want example: ---> I didn’t understand it well, could explain it better ?

  • example <input type='text' name='datafinal' id='datafinal' <?php echo $variaveldobanco['datafinal']? >class='form-control'>

  • you want to get the date we see from the bank query or a date you’re sending to the bank?

  • The date I am sending - The person puts there the period he wants to consult and with that I need the period to continue there

  • Now understood the person puts a break and query the type of occurrence is this? If that’s it you can use the post when consulting the test knife, see the answer fill in the date and click on Ubmit. When the page is loaded you can take the post data and return in the inputs

  • @otaciojb I just edited my answer forgot the method='post', if any of the answers are correct, consider validating it.

Show 2 more comments

3 answers

1


As your goal and present the dates after the query you can recover these data by the post variable as shown below. If you doubt a var_dump($_POST) and you will see the whole post you did

<?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>

        ?>
  • 1

    <br /><b>Notice</b>: Undefined index: datafinal in <b>/var/www/html/system/main.php</b> on line <b>134</b><br /> , how one starts without date returns it, and even after the query continues to return it

  • this is because the post variable is empty it will only be filled after Submit

  • @Thallesdaniel For this not to happen, we can define the variables of the type string with a space in arms space, note not just simple quotes it is necessary space.

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>";

 ?>

0

You can recover the data as follows, as already mentioned in the previous answers, through the global variable $_POST.

Regarding the error referred to in the comments in the previous reply :

<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 previously define the variables of the string type with a blank space 'espaço', note not just simple quotes it is necessary space.

<?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 :Why should we not use mysql type functions_*?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.