有人可以告诉我为什么这段代码不起作用?我正在尝试从php文件中的表单中发布一个值,该值使用此值在我的数据库中进行选择。将Json数组返回给ajax并打印所有结果。
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>
函数enviar():
<script>
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');
}
});
});
}
</script>
我收到错误:enviar未定义...
答案 0 :(得分:0)
首先,在进行连接的情况下,变量内容格式不正确。其次,看起来你也有不匹配的牙箍。第三,$ .ajax()应该使用键/值对来设置你想要做的AJAX请求,但是你有一个变量声明。你应该重新检查你的整个代码。