1
Can anyone help me with this code? Why doesn’t it work? I am trying to send a value received by a form to a php file, use this value in my SQL query searching for records that contain this value, return a Json array with the result(s) and print them on the screen using html.
My Query SELECT:
$sql= "SELECT * FROM incidente WHERE (titulo LIKE '%':buscar'%' OR descricao LIKE '%':buscar'%')";
//...
$recebeConexao->bindParam(':buscar', $_POST['busca'], PDO::PARAM_STR);
HTML code:
<!-- here I am creating a form whit text input and a button that call the function enviar() -->
<form id="buscar">
<input id="busca" name="busca" type="text" placeholder="Buscar incidente" />
<input onclick="enviar()" type="button" value="ok" />
</form>
<!-- creating a array that will receive values from SQL consult -->
<div id="content" class="content">
<article class="underline">\
<a href="incidente.html"><img id="incidente"\
src="img/buraco.jpg" alt="Incidente" /></a>\
<h2><a href="basic_markup.html" id="tit">'+tit+'</a></h2>\
<p id="desc">'+desc+'</p>\
<div class="date" id="date">'+dateVal+'</div>\
<img class="tick" alt="não resolvido" src="img/no-tick.png">\
<img class="apoio" alt="apoiar" src="img/apoio.png">\
</article>'
</div>
Send function():
function enviar() {
function viewData(data, el) {
var content = '';
for (var i in data) {
var tit = data[i].titulo;
var desc = data[i].descricao;
var dateVal = data[i].data;
content += '<article class="underline">\
<a href="incidente.html"><img id="incidente"\
src="img/buraco.jpg" alt="Incidente" /></a>\
<h2><a href="basic_markup.html" id="tit">'+tit+'</a></h2>\
<p id="desc">'+desc+'</p>\
<div class="date" id="date">'+dateVal+'</div>\
<img class="tick" alt="não resolvido" src="img/no-tick.png">\
<img class="apoio" alt="apoiar" src="img/apoio.png">\
</article>';
}
$('#'+el).html(content);
}
$(function(){
$.ajax({
var formula = $('#buscar').serialize();
type: "POST",
data:formula,
url: "http:/ip/connect/www/buscar.php",
dataType: "json",
success: function (data) {
viewData(data,'content');
}
});
});
}
I’m getting the bug: enviar is not defined
...
Where on the page you are declaring the function
enviar();
? This error is because the page is trying to call a function that does not exist or has not yet been declared.– Lucas Fontes Gaspareto
viewData()
is set withinenviar()
? What’s the problem json isn’t enough? your query returns nothing?– rray
You placed the function viewData inside the send function, and called the Viewdata function without calling the send function. So it is the same as saying that the function viewData did not exist. And gives error.
– Ivan Ferrer