Javascript 从客户端使用变量加载文件
我需要在容器中加载一个文件,但使用一个参数-首先从数据库中获取一些数据:Javascript 从客户端使用变量加载文件,javascript,php,jquery,Javascript,Php,Jquery,我需要在容器中加载一个文件,但使用一个参数-首先从数据库中获取一些数据: $('#story').load('test.php'); test.php $st = $db->query("select * from users where id = " . $id); ... processing variables... load the finished content 这里我需要客户端的$id。可能吗?是..您可以通过url查询传递 $('#story').load('te
$('#story').load('test.php');
test.php
$st = $db->query("select * from users where id = " . $id);
... processing variables... load the finished content
这里我需要客户端的
$id
。可能吗?是..您可以通过url查询传递
$('#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:
可能重复的注意事项是,不要直接将变量注入SQL字符串中,要非常小心。->你应该使用事先准备好的语句,也许还要做一些检查first@Kaddath关于select
语句的sql注入?是的,这可以用来获取数据库中其他表的信息,使用join等,这是一个好习惯anyway@Kaddath谢谢我认为select语句是安全的,您可以使用null coalesce运算符代替ternery运算符作为$id=$\u请求['id]??''@RohitGhotkar您是对的。但它只在PHP7及以上版本上受支持。在较低版本上不受支持
<?php
$id = $_POST['id'];