我正在创建图片库,首先,它基本上从PHP加载服务器图像的缩略图,并将 alt 属性设置为文件夹的路径,然后,我正在尝试当用户点击其中一个图像时,它应该加载该文件夹中的所有图像,但事实并非如此。我使用jQuery,这是简化的代码。我只需要指定PHP变量的路径。我尝试了所有我知道的但是没有用。
使用PHP发送文件路径
$(document).ready(function(){
$("html").on("click", ".gcube img", function() {
var $path = $(this).attr('alt');
$.post("TESTLOAD.php", { link: $path} );
});
});
TESTLOAD.php文件:
<?php
$link = $_POST['link'];
echo $link;
//other stuff that will load images will be here
?>
更新
好的,这是缩小的代码,足以理解
发送数据的文件中的代码(TESTPOST.php):
path = "projects/first/pic.jpg";
$.post("TESTLOAD.php", { link: path} );
代码应该接收数据,但它不会,或者至少它没有回应任何东西:
$link = $_POST['link'];
echo '<div id="blah"> ' . json_encode(var_dump($_POST)) . '</div>';
//I was hoping that after I $.post("TESTLOAD.php", { link: path} ); it even echoes out something but it really does nothing
如果我只是在jquery中使用post,它没有回应,所以我必须再次加载
$("#output").load("TESTLOAD.php");
但我想如果我再次加载它,变量就不会再保存在那里了。知道什么是错的吗?
答案 0 :(得分:0)
好的,我知道出了什么问题我感到很愚蠢:D我不知道我可以使用jQuery加载功能并立即发送数据。现在我根本没有使用帖子,只有这一行$("#output").load("TESTLOAD.php", {link:path});
输出:Div我要显示加载的数据/ TESTLOAD.php文件,我要加载{link:path}:我要发送给php的数据。