Alexandre Cristo Alexandre Cristo - 7 days ago 6
MySQL Question

Best way to create HTML code from a query on another page

I have this page with this form:

index.php

<form role="form" action="#" method="post">
<h3 class="title-form">Pesquisa avançada</h3>
<div class="form-group">
<select class="form-control form-text" id="negocio">
<option value="" disabled selected>Tipo de Negócio</option>
</select>
</div>
<div class="form-group">
<select class="form-control form-text" id="imovel">
<option value="" disabled selected>Tipo de imovel</option>
</select>
</div>
<div class="form-group">
<select class="form-control form-text" id="localidade">
<option value="" disabled selected>Tipo de localidade</option>
</select>
</div>
<button type="submit" class="btn submit-btn" style="margin-top: 0px;">Pesquisar</button>
</form>


After the client completes this form, it should query the database and return the results. Those results must be shown on another webpage and they have to create html code depending on the number of results that the query returns:

Example:

imovel.php (Page with 1 result from query)

<body>
<p id="test"> Result 1 </p>
</body>


imovel.php (Page with 2 result from query)

<body>
<p id="test"> Result 1 </p>
<p id="test"> Result 2 </p>
</body>


So, my question is what is the best way to query a database from a multiple value form and send the results from that query to another PHP webpage in order to create html code based on those results.

Answer

Forms have the Action attribute, it sends the posted information to the url in it. So, put in this attribute imovel.php.

In imovel.php code, put the php code that obtains the POST information ($_POST[] superglobal), and then do your queries.

<?php
    if(isset($_POST){
        // ... do here your form processing.

        $results = your-method-to-fetch-results-from-database()
    }
?>

Now you have the $results variable that holds all the results from database. Just do a foreach loop using it inside your <body> and applying the desired markup

Example:

<?php foreach($results as $item_result){ ?>
<p>Name: <?php $item_result['name']; ?>
<?php } ?>
Comments