简体   繁体   English

AJAX:发布值,使用该值进行查询并返回Json数组

[英]AJAX: Post value, make a consult using this value and return a Json array

Can someone tell me why this code don't work? 有人可以告诉我为什么此代码不起作用吗? I am trying post a value from form in a php file that make a select in my DB using this value. 我正在尝试从php文件中的表单中发布一个值,该文件使用此值在我的数据库中进行选择。 Return a Json array to ajax and print all results. 将Json数组返回到ajax并打印所有结果。

SELECT query: SELECT查询:

...

$sql= "SELECT * FROM incidente WHERE (titulo LIKE '%':buscar'%'   OR 
descricao LIKE '%':buscar'%')";

...

 $recebeConexao->bindParam(':buscar', $_POST['busca'], 
 PDO::PARAM_STR); 

HTML code: HTML代码:

 //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>

Function enviar(): 函数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>

I am receiving the error: enviar is not defined... 我收到错误:未定义enviar ...

First, the variable content is not properly formatted, where you do the concatenation. 首先,变量内容的格式不正确,您无法在其中进行串联。 Second, it looks like you also have mismatched braces. 其次,看起来您的括号也不匹配。 Third, $.ajax() should take key/value pairs to set up the AJAX request you are trying to make, but you have a variable declaration. 第三,$ .ajax()应该使用键/值对来设置您要发出的AJAX请求,但是您有一个变量声明。 You should recheck your whole code. 您应该重新检查整个代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM