How to persist / fill the data of a form via PHP?

2
... /busca?tipo%5B%5D=APARTAMENTO%2FAPTO+DUPLEX&area_de=50&area_at=100

In this URL I have 2 data types:

type that is checkbox

  • APARTMENT
  • FIT + DUPLEX

area_of which is select

  • 50

area_at which is select

  • 100

How do I persist this data after the search? How to make them checked or selected so that the search is not requested and that it is enough to give another search with one or another different parameter without having to reschedule everything again?

Update

Dear, the HTML is very extensive, because I put a part in the question to be able to solve the rest based on the response, as requested, here is the HTML . This is the HTML of the entire search.

Form HTML Code

<form action="/busca" method="GET" name="frm_busca" id="frm_busca">
    <fieldset class="w255">

        <legend>Tipo</legend>

        <input type="checkbox" name="tipo[]" value="APARTAMENTO/APTO DUPLEX" id="tp1">
        <label for="tp1">Apartamento</label>

        <input type="checkbox" name="tipo[]" value="CASA" id="tp2">
        <label for="tp2">Casa</label>

    </fieldset>
    <fieldset class="w145">

        <legend>Dormitórios</legend>

        <input type="checkbox" name="dorm[]" value="1" id="dorm1">
        <label for="dorm1">1</label>

        <input type="checkbox" name="dorm[]" value="2" id="dorm2">
        <label for="dorm2">2</label>

        <input type="checkbox" name="dorm[]" value="3" id="dorm3">
        <label for="dorm3">3</label>

        <input type="checkbox" name="dorm[]" value="4" id="dorm4">
        <label for="dorm4">4+</label>

    </fieldset>
</form>
    
asked by anonymous 01.11.2014 / 22:56

2 answers

5

Just by the desired value in the value of the text fields, or option checked and selected in the checkboxes and selects

Example for text field:

$nome = $_GET['nome'];
$form_nome = htmlentities( $nome );

echo '<input type="text" name="nome" value="'.$form_nome.'">';

Example for select :

$metragem = $_GET['metragem'];

// EXEMPLO DIDATICO APENAS! Na prática pode ser switch ou um loop inteligente
// atrelado a um loop que gere o form, por exemplo, ou if ... elseif, etc:
if( $metragem == '100' ) $form_select100 = ' selected="selected"';
if( $metragem == '200' ) $form_select200 = ' selected="selected"';

echo '<select name ="metragem">';
echo '   <option value="100"'.$form_select100 .'>100m</option>';
echo '   <option value="200"'.$form_select200 .'>200m</option>';
echo '</select>';

Example for checkbox :

$avisar = $_GET['avisar'];

if( $avisar == 'sim' ) $form_avisar = ' checked="checked"';

echo '<input type="checkbox" name="avisar" value="sim"'.$form_avisar.'>Avisar</input>';

You just have to adapt to the specific case, and apply some basic logic to not doing field by field manually (that's a general programming question).

Important:

  • The "long" version comes to the case only for compatibility with XML serialization, in HTML5 you can use the short version quietly:

    Em vez de:                                Pode usar simplesmente:
    <option value="x" selected="selected">    <option value="x" selected>
    <input id="abcde" checked="checked">      <input id="abcde" checked>
    
  • In this case, remember to sanitize the text values with htmlentities() before in value .

  • checkboxes remember that you have to separately identify each of them to avoid confusion, since empty checkboxes are not sent.
  • Note that we always leave a blank space to separate from the previous attribute:

    $ch1 = ' checked="checked"'
            ^
    

Here's another example, for use of arrays . Similarly, didactic example, in the practical case can use an internal loop , or even switch. It depends on each case.

foreach( $dorm as $item ) {
    // apenas exemplo. use um loop no caso concreto.
    if( $item == '1' ) $ch1 = ' checked="checked"';
    if( $item == '2' ) $ch2 = ' checked="checked"';
    if( $item == '3' ) $ch3 = ' checked="checked"';
    if( $item == '4' ) $ch4 = ' checked="checked"';
}

echo '<input type="checkbox" name="dorm[]" value="1" id="dorm1"'.$ch1.'>';
echo '<input type="checkbox" name="dorm[]" value="2" id="dorm2"'.$ch2.'>';
echo '<input type="checkbox" name="dorm[]" value="3" id="dorm3"'.$ch3.'>';
echo '<input type="checkbox" name="dorm[]" value="4" id="dorm4"'.$ch4.'>';

And here's an inline version of the same logic:

echo '<input type="checkbox" name="dorm[]" value="1" '.($item=='1'?' checked':'').'>';
    
01.11.2014 / 23:20
3

Separating the problem into parts:

If you really want to do this in JavaScript you need more information in this query string. Any% re_% that is repeated can not know which is which. Here's an idea to help:

To read query string information:

function getHASH() {
    var hash = window.location.search.slice(1);
    var pares = hash.split('&');
    var chaves = pares.map(function (par) {
        var chave_valor = par.split('=');
        return {
            chave: decodeURI(chave_valor[0]),
            valor: decodeURI(chave_valor[1])
        };
    });
    return chaves;
}

With this function you will receive an array of objects, in the example you put it like this:

"[
    {
        "chave": "tipo[]",
        "valor": "APARTAMENTO%2FAPTO+DUPLEX"
    },
    {
        "chave": "area_de",
        "valor": "50"
    },
    {
        "chave": "area_at",
        "valor": "100"
    }
]"

To iterate your HTML by applying these values:

var pares = getHASH();
pares.forEach(function (par) {
    var chave = Object.keys(par)[0];
    var valor = Object.keys(par)[1];
    $('input[name="' + chave + '"]').filter(function () {
        return ~this.value.indexOf(valor);
    })[0].checked = true;
});

With this code you will look for all inputs and check which of them has in your tipos[] the value that comes from the query string and mark it as value

    
01.11.2014 / 23:24