我需要在容器中加载一个文件,但是使用一个参数 - 首先从数据库中获取一些数据:
$('#story').load('test.php');
test.php的
$st = $db->query("select * from users where id = " . $id);
... processing variables... load the finished content
在这里,我需要来自客户端的$id
。可能吗?
是的..你可以通过url query
$('#story').load('test.php?id=1');
test.php的
$id = isset($_REQUEST['id'])?$_REQUEST['id']):'';
$st = $db->query("select * from users where id = " . $id);
您可以使用ajax请求,并在成功时加载文件,例如:
$.ajax({
type: 'POST',
url: "test.php", //make sure you put the right path there
data: {id: id},
dataType: "json",
success: function (resultData) {
$('#story').load('test.php');
}
})
只需确保你的php函数返回/回显你想要的id。
这样你就可以调用你的php文件,当它成功时,你将加载你的文件,如果你想返回更多的数据来使用它,你可以在那里添加额外的逻辑。
resultData
保存你的php函数的输出,所以它取决于你想要在那里的信息。
您可以使用Ajax将ID
发布到您的PHP代码中。
$.ajax({
type: "POST",
url: "test.php",
data: { 'id': foo },
cache: false,
success: function(){
alert("Order Submitted");
}
});
PHP:
<?php
$id = $_POST['id'];