如何在php文件中获取$.post-ajax传递的值


how to get the value passed by $.post ajax in php file

我的Ajax调用类似于

    var txt=$('#keyword').value;
    $.post("ajax.php?for=result", {suggest: "keyword="+txt}, function(result){
   $("#search_result").html(result);
     });

在我的php文件中,我想获得id为"keyword"的文本框的值,它像一样传递

var txt=$('#keyword').value;
$.post("ajax.php?for=result", {suggest: "keyword="+txt},

我在php文件中尝试使用$_POST和$_GET方法,但它给了我一个类似"未定义索引"的错误

我们如何在php文件中获得该值?。还为我提供了一个如何使用json实现这一点的示例。

您没有正确发布值。

发布值的正确方式是作为普通对象

var txt=$('#keyword').value;
$.post("ajax.php?for=result", {keyword: txt}, function(result){
   $("#search_result").html(result);
});

作为键=用'&'分隔的值的字符串

var txt=$('#keyword').value;
$.post("ajax.php?for=result", "keyword="+txt, function(result){
   $("#search_result").html(result);
});

js:

 var txt=$('#keyword').value;
    $.post("ajax.php?for=result", {suggest:txt}, function(result){
   $("#search_result").html(result);
     });

ajax.php:

$keyword = $_POST['suggest'];