-1
Hello, I’m developing a web project but I can’t get the desired result. I have a php file that returns me an array of vehicles and my Index calls this file to get this array through ajax. When searching in Index, it returns the part of the matrix that corresponds to what I looked for, but I would like to fill the table with the data and not print a pure array on the page. Thanks to those who help !!
My code:
$(document).on('click', '#pesquisa', function () {
showPesquisa();
clearResponse();
});
// show home page
function showPesquisa() {
var html = `
<style>
body {background-image: url("imagens/5series.jpg");
background-size: cover;
color: #FFFFFF
</style>
<h2>Pesquisar</h2>
<form id='search_form'>
<div class="form-group">
<input type="text" class="form-control" name="s" id="s" required placeholder='insira o seu nome'/>
</div>
</form>
<button type="button" class="btn btn-warning" id="search_btn">Pesquisar</button>
<div class="container">
<table class="table table bordered" id="veiculosTable">
<thead>
<tr>
<th>Identificador</th>
<th>Categoria</th>
<th>Marca</th>
<th>Modelo</th>
<th>Lugares</th>
<th>€/dia</th>
<th>Combustível</th>
<th>Portas</th>
<th>Caixa</th>
<th>Empresa</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
`;
clearResponse();
$('#content').html(html);
};
//$("#search_btn").click(function(){
$(document).on('click', '#search_btn', function () {
var search_form = $("#search_form");
var search_data = JSON.stringify(search_form.serializeObject());
$.ajax({
url: 'api/veiculo/search.php',
type: "POST",
data: search_data,
dataType: 'JSON',
success: function(response) {
$('#response').html("<div class='success'>"+JSON.stringify(response)+'</div>');
},
error: function (xhr, resp, text) {
// on error, tell the user sign up failed
$('#response').html("<div class='alert alert-danger'>Erro de pesquisa: "+xhr+" "+resp+" "+text+'</div>');
}
});
});
Good friend, thanks for the help, but unfortunately it did not work... He presented the results over the table and deformed... I wonder what I can do to improve?
– Helder Santos
The
id="response"
should be only in thetbody
. That must be the cause of the problem.– Sam
Buddy, I made a change. Put it on
tbody
another id:id="resposta"
and leave the other element with theid="response"
as was.– Sam
It worked friend!!!!! I did <tbody id="answer"></tbody> and I made $('#answer'). html(vehiclesTable);
– Helder Santos
How do I do it buddy?
– Helder Santos
Already you are friend, once again thank you very much!! Great hug
– Helder Santos