我不知道为什么,但是 jQuery 没有将参数传递给 PHP,以便根据参数 ID 使用特定数据完成 load() 函数。
短期内,当用户单击链接时,会触发 onclick photo_tab('.$row['id'].');
account.php HTML 元素
<li><a href="#!/photos" onclick="photo_tab('.$row['id'].');"><i class="icon-camera"></i> Photos</a></li>
从某种意义上说,ID 应该传递到 photo_tab(脚本包含在页面中,而不是页面本身)
通用.js
function photo_tab(user_id) {
$('.ac-content').load('ajax/photos.php',{user_id:id});
}
应该加载 photos/页面,这是一个 PHP 页面,并根据 user_id 值从与该 user_id 关联的数据库中加载照片。
<?php
if ($_GET['user_id'] == 2) {
echo "WORKED!";
} else {
echo "FAILED!";
}
***this is the photos/ file located at ajax/photos.php with mod_rewrite to point
otherwise to just photos/ or photos
编辑:1 - 使用完整路径而不是重写照片 > ajax/photos.php 不起作用 2 - ID 正在通过 $row['ID'] 传递并回显到页面上; 3 - ajax/photos.php 使用 $_REQUEST
最佳答案
尝试将数据作为查询字符串传递,如下所示:
function photo_tab(user_id) {
$('.ac-content').load('photos/','user_id='+id);
}
编辑: 再看一下,这个 URL 正确吗?有相对 URL 似乎很奇怪。
您还应该确保生成的标记正确,行 ID 是否在源中打印出来?
编辑2: 我在本地进行了设置,这是工作代码,模仿了您的文件结构。
//localhost/account.php
<html>
<head>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
function photo_tab(id) {
$('.ac-content').load(
'ajax/photos.php',
{ 'user_id': id }
);
}
</script>
</head>
<body>
<div class="ac-content"></div>
<a href="#!/photos" onclick="photo_tab(3);"><i class="icon-camera"></i> Photos</a>
</body>
//localhost/ajax/photos.php
<?php
if (isset($_GET['user_id']) && $_GET['user_id'] == 2)
{
echo "WORKED!";
}
else
{
echo "FAILED!";
}
关于php - jQuery 参数未传递给 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14533920/