【发布时间】:2015-08-07 13:59:51
【问题描述】:
我已经研究过这个问题(我阅读了很多 StackOverflow 问题),但我仍然无法找到解决方案。
当我单击链接时,我使用 jQuery 获取 id HTML 'a' 属性值,然后我需要将其发送到将执行数据库查询的 php 文件。我尝试使用 $.ajax 和 $.post jQuery 函数,我得到了同样的错误。
与$.post
var idElement;
$(document).ready(function(){
$('.linkElement').click(function(){
idElement= $(this).attr('id');
console.log(idElement); // Shows id value on console
$.post("showElement.php", idElement).done(function (){
alert("All went good");
});
$(document.body).load("showElement.php");
});
});
与$.ajax
var idElement;
$(document).ready(function(){
$('.linkElement').click(function(){
idElement= $(this).attr('id');
console.log(idElement); // Shows id value on console
$.post("showElement.php", idElement).done(function (){
alert("All went good");
});
$.ajax({
url: "showElement.php",
type: "POST",
data: {'idElement':idElement}
});
});
});
当我在 showElement.php 文件中使用 $_POST['idElement'] 时,我得到以下信息:
Notice: Undefined index: idElement in C:\xampp\htdocs\path\to\showElement.php on line 28
我做错了什么?
编辑:我用 Firebug 对此进行了测试,它说没问题。 Firebug 控制台中显示的数据正确显示,但 PHP 文件未显示在浏览器中(var_dump 返回应具有的值)。
对此有什么想法吗?
【问题讨论】:
-
代替
method:"POST",使用type:"POST" -
感谢您的回复。我就像你说的那样,在 jQuery 网站上搜索后,我注意到它可能应该是“方法”而不是“类型”,所以我改变了它。如果我将其保留为“类型”,则不会显示 showElement.php。有什么我应该知道的吗?
-
在您的
showElement.php中尝试回显$_POST['idElement'] -
也可以使用
PHP代码编辑您的帖子
标签: javascript php jquery ajax