如何使用ajax将数据发布到php文件并获得结果


How to post data to php file and get result using ajax?

嗨,我想将数据发布到php,并将php结果和结果发布重置到htmldiv标记。请给我样本代码。谢谢你,抱歉我的英语不好

好吧,假设你可以在点击link类的a标签时发出AJAX请求,HTML看起来像:

<div class="content"></div>
<a href="#" class="link">Ajax Request</a>

JavaScript:

$('.link').click(function(){       
    $.ajax({  
        method: 'GET',  
        url: 'ajax.php',
        data: { 'data_id': 'YOUR_DATA' }, //Data you want to send
        success: function(response) {
           $(".content").append(response);
        }
});

ajax.php:

$data=$_GET["data_id"];
//Do some process and echo what you want to sendback
echo $data; 

注意:您必须在文档中包含jQuery