Keep data in the PHP form textbox | HTML

Asked

Viewed 161 times

0

I have a form, but when I submit the form, all the data in the textboxes disappear, I want to know how to keep the data in the textboxes after submitting, so that the user does not have to enter the data all over again.

<form method="post"> 
<div class="col-md-4">  
<label>Referencia: </label> 
<input type="text" placeholder="&#xF002; Search" name="referencia"style="font-family:Arial, FontAwesome"> 
    </div>
    <div align="right"> 
    <img src="imagens/desenho33.png" align="right" height="250" width="350" > 
    </div>
<br> 
<div class="col-md-4">     
<label>Diametro do Aço (d): </label> 
<input type="text" name="d1" size="6" placeholder="min">
<input type="text" name="d2" size="6" placeholder="max">
</div> 
<div class="col-md-4">
<label>Comprimento total (L0): </label>
<input type="text" name="comp1" size="6" placeholder="min"> 
<input type="text" name="comp2" size="6" placeholder="max">
</div> 
<div class="col-md-4">     
<label>Diametro Exterior (DE): </label>
<input type="text" name="de1" size="6" placeholder="min"> 
<input type="text" name="de2" size="6" placeholder="max"> 
</div>  
<br> 
<div class="row"> 
<div class="col-md-4">     
<div class="form-group"> 
<label for="exampleFormControlSelect1">Ordenar por:</label> 
<select class="form-control" name="ordenar" id="ordenar"> 
<option value="diametroaco">Diametro do Aço (d)</option> 
<option value="comprimentototal">Comprimento total (L0)</option> 
<option value="diametroexterior">Diametro Exterior (DE)</option> 
</select> 
</div> 
</div>
</div> 
<br> 
<div align="right"> 
<button type="reset" align="right" class="btn btn-primary">Reset</button> 
<button type="submit" align="right" class="btn btn-primary" value="submit">Continuar</button> 
</div> 
</form>

<?php
    if(isset($_POST)&&!empty($_POST)){
        if ($_POST["d1"]<>""){
            $d1=$_POST["d1"];
        }else{
            $d1="0,200";
        }
        if ($_POST["d2"]<>""){
            $d2=$_POST["d2"];
        }else{
            $d2="20";
        }
        if ($_POST["comp1"]<>""){
            $comp1=$_POST["comp1"];
        }else{
            $comp1="1";
        }
        if ($_POST["comp2"]<>""){
            $comp2=$_POST["comp2"];
        }else{
            $comp2="10000";
        }
        if ($_POST["de1"]<>""){
            $de1=$_POST["de1"];
        }else{
            $de1="1";
        }
        if ($_POST["de2"]<>""){
            $de2=$_POST["de2"];
        }else{
            $de2="200";
        } 
//var_dump($_POST);
        $ordenar=$_POST["ordenar"];

        //if(isset($_POST["getproduct"])){
        //$ordenar="nome";
        //}
        switch($ordenar){
            case "diametroaco":
                $ordenar_por="ORDER BY diametroaco";
                break;
            case "comprimentototal":
                $ordenar_por="ORDER BY comprimentototal";
                break;
            case "diametroexterior":
                $ordenar_por="ORDER BY diametroexterior";
                break;
        }

        include ("db.php");
        // $molcomp_query="SELECT * FROM stock_comp";
           //$molcomp_query="SELECT * FROM stock_comp $ordenar_por";
        $molcomp_query="SELECT * FROM stock_comp WHERE (diametroaco BETWEEN '$d1' and '$d2') AND (comprimentototal BETWEEN '$comp1' AND '$comp2') AND (diametroexterior BETWEEN '$de1' AND '$de2') $ordenar_por";
        
         $run_query = mysqli_query($con,$molcomp_query);
        if (mysqli_num_rows($run_query) > 0){
            while($row = mysqli_fetch_array($run_query)){
                $id_mol_comp=$row['id_mol_comp'];
                $referencia=$row['referencia'];
                $diametroaco=$row['diametroaco'];
                $comprimentototal=$row['comprimentototal'];
                $diametroexterior=$row['diametroexterior'];
                $passo=$row['passo'];
                $preco=$row['preco'];
                echo
                "<div class='card mx-auto w-100'>
                    <div class='card-body'>
                            <div class='row'>
                            <div class='col-md-1' style='text-align: center'>$referencia</div>
                            <div class='col-md-2' style='text-align: center'>$diametroaco</div>
                            <div class='col-md-2' style='text-align: center'>$comprimentototal</div>
                            <div class='col-md-2' style='text-align: center'>$diametroexterior</div>
                            <div class='col-md-1' style='text-align: center'>$passo</div>
                            <div class='col-md-1' style='text-align: center'>$preco €</div>
                            <div class='col-md-1' style='text-align: center'><i style='font-size:30px' class='fa'>&#xf1c1;</i> <i style='font-size:30px' class='fa'>&#xf217;</i> </div>
                        </div>
                    </div>
                </div>
                ";
           }
        }
    }
?>

2 answers

2

You must fill in these fields with the request data POST / GET
Assuming you’re using the method POST

<input type="text" name="nome" value="<?php echo $_POST[nome]; ?>"/>

Same functionality, only shorter
<input type="text" name="nome" value="<?= $_POST[nome]; ?>"/>

  • I already added the code in the question, I am using the POST, but not in this way.

  • You are using AJAX?

  • no, but it is necessary?

  • No, it is not necessary. It will depend on your need. How are you doing the submission of the form data? How do you handle it after submitting?

  • I already asked the question

  • My answer solves your problem, what is your difficulty?

  • is that it generates an error: Notice: Use of Undefined Constant de2 - assumed 'de2' in (de2 i replaces the name )

Show 3 more comments

1

This happens because every time you submit the form, the page reloads, so we need to make an asynchronous request, ie make a post without reloading the page.

First add the following parameters in the form tag:

<form method="post" action="" id="idDoForm">

Notice that action="" is blank, this prevents it from doing a standard action, we want it to do nothing, who will work for us is a script.

Then import the Jquery library, just download or import via CDN:

https://jquery.com/

Then import or create a new script on the page itself: (This script should be loaded after Jquery import)

$("#idDoForm").submit(function () {
    $.ajax({
        type: 'post',
        url: 'caminhoDoProcessadorPHP', //Exemplo: 'includes/post.inc.php'
        data: $("#idDoForm").serialize(),
        success: function(data) {
            //Lembre-se que a variável data vai retornar a resposta do arquivo 'post.inc.php' se vc escrever "echo 'Hello, World!';", a variável data vai conter "Hello, World!", então crie <div id="response"></div> em qualquer lugar da página onde vc quer que essa variável retorne a resposta
           $("#response").html(data);
        }
    });
    return false;
}

This way when pressing the "Continue" button the script takes the form data and passes to your php file that processes the data and gives an answer without reloading the page.

  • it’s not working, it’s a mistake, I don’t know anything about ajax, there’s no other way to do it?

  • put the error here in the comments, check also the console (F12) and see if there are any errors to put here too, in the 'Network' tab check the requests and see the php file if there are any parameters being passed in 'param'

Browser other questions tagged

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