AJAX POST:回显已发布的锚标签值 [英] AJAX POST: echoing posted anchor tag value
本文介绍了AJAX POST:回显已发布的锚标签值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HTML/jQuery:
HTML/jQuery:
<a href=# id="friends">Friends</a>
<script type="text/javascript">
$(document).ready(function() {
$('a#friends').click(function() {
$.ajax({
type: "POST",
url: "data.php",
data: $('#friends').html(),
success: function(data) {
$('#questions').html(data);
},
dataType: "HTML"
});
});
});
</script>
data.php:
<?php
echo $_POST['#friends'];
?>
如何在锚标记中返回ID的POST
值?该变量正在传递给PHP,因为我可以发出警报,但问题是将其找回.
How do I return this POST
value of an id in an anchor tag? The variable is being passed to PHP because I can alert it, but the problem is getting it back.
推荐答案
您需要指定要在AJAX请求中发送的值的名称.试试这个:
You need to specify the name of the value you are sending across in your AJAX request. Try this:
$.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
echo $_POST['friends']; // retrieve the 'friends' value
?>
这篇关于AJAX POST:回显已发布的锚标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文