简体   繁体   中英

How to fix this undefined index error? Ajax to PHP

Notice : Undefined index: query in on line 行的
{"names":[]}

This is my Php

$query=$_POST["query"];
$matchType=isset($_POST["match_type"])? $_POST["match_type"]:MatchType::CONTAINS;

processRequest($query,$matchType);

Here is my ajax script

$("#query").keyup(function(){
    var q=$(this).val();
    var match_type=$("input[type=radio]:checked").val();
    var data={'query':q,'match_type':match_type};
    if(q.length==0){
        $("#results").html("");
        return false;

        $.ajax({
            url:"/Java/Search/instant-search.php",
            data:data,
            type:"post",
            dataType:"json",
            success:function(res)  {
                var tmpl=$("#names_tmpl").html();
                var html=Mustache.to_html(tmpl,res);

                $("#results").html(html);
            }
        });

As per your comments if both of them match_type and q are getting values than use ajax with data as:

$.ajax({ 
   url:"/Java/Search/instant-search.php", 
   data: "query="+q+"&match_type="+match_type , 
   type:"post", 
   dataType:"json", 
   success:function(res) { 
     var tmpl=$("#names_tmpl").html(); 
     var html=Mustache.to_html(tmpl,res);
     $("#results").html(html); 
   } 
});

To change the type of request use method property not type and use Uppercase string POST jQuery.ajax :

   $.ajax({
        url: "/Java/Search/instant-search.php",
        data: data,
        method: "POST",
        dataType: "json",
        success: function(res) {
            var tmpl=$("#names_tmpl").html();
            var html=Mustache.to_html(tmpl,res);

            $("#results").html(html);       
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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