AJAX POST:回显已发布的锚标记值

HTML / jQuery的:

Friends  $(document).ready(function() { $('a#friends').click(function() { $.ajax({ type: "POST", url: "data.php", data: $('#friends').html(), success: function(data) { $('#questions').html(data); }, dataType: "HTML" }); }); });  

data.php:

  

如何在锚标记中返回id的POST值? 该变量正在传递给PHP,因为我可以提醒它,但问题是将其恢复。

您需要在AJAX请求中指定要发送的值的名称。 试试这个:

 $.ajax({ type: "POST", url: "data.php", data: { 'friends': $('#friends').html() }, // Note the value is sent in an object with a key of 'friends' success: function(data) { $('#questions').html(data); }, dataType: "HTML" }); 
  

如何将数据传递给PHP,

请使用以下代码,

 Friends   

将friend值传递给data.php的语法错误

试试这个

 $(document).ready(function() { $('a#friends').click(function() { $.ajax({ type: "POST", url: "data.php", data: "friends="+$('#friends').html(), success: function(data) { $('#questions').html(data); }, dataType: "HTML" }); });  

首先,您无法以这种方式将数据发送到ajax页面

 data: $('#friends').html(), 

一种更合适的方式

 data : {'key1':'val1', 'key2':'val2'} 

然后在php页面上,您可以以这种方式检索这些值

 $key1 = $_POST['key1']; // will contain 'val1' $key2= $_POST['key2']; // will contain 'val2' 

或者你可以使用

 Friends  

并在PHP中:

  

在数据字段中传递数据变量。 有关详细信息,请参阅以下示例

 $(document).ready(function() { $('a#friends').click(function() { alert(""); $.ajax({ type: "POST", url: "data.php", data: "#friends="+$('#friends').html(), success: function(data) { alert(data); $('#questions').html(data); }, dataType: "HTML" }); }); });