How to persist/fill in form data via PHP?

Asked

Viewed 1,097 times

2

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

In this URL I have 2 types of data:

kind of is checkbox

  • APARTMENT
  • APTO+DUPLEX

area_de que é select

  • 50

area_at that is select

  • 100

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

Updating

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

HTML code of the form

<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>
  • You can put HTML in as well?

  • HTML placed. Thanks for the promptness.

  • 4

    Nothing prevents you from reducing HTML to the essential minimum for those reading the question can answer without having to go to another site, have two windows open to be able to read the problem and see the code... your job is to make life easier for the respondent... How to create a Minimum, Complete and Verifiable example

  • 4

    I’m done giving tips. Io finite sleep.

2 answers

5


Just for the desired value on value text fields, or option checked and selected us checkboxes and selects desired.

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

Then just adapt to the specific case, and apply some basic logic not to do field to field manually (there is already a question of programming in general).

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 the practical case, remember to sanitize the text values with htmlentities() before putting on value.

  • us checkboxes remember that you have to separately identify each of them to avoid confusion, since checkboxes empty are not sent.

  • note that we always leave a blank space to separate from the previous attribute:

    $ch1 = ' checked="checked"'
            ^
    

Here is another example, for use of arrays. Similarly, didactic example, in the practical case can use a loop internal, 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 a version inline of the same logic:

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

3

Separating the problem into parts:

If you really want to do this in Javascript you need more information in this query string. All tipos[] that are repeated can not know which is which. However here is an idea to help:

To read the 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 gives so:

"[
    {
        "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 search all inputs and check which one has in your value the value that comes from the query string and mark it as checked

Browser other questions tagged

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