使用客户端变量加载文件

问题描述 投票:4回答:3

我需要在容器中加载一个文件,但是使用一个参数 - 首先从数据库中获取一些数据:

$('#story').load('test.php');

test.php的

$st = $db->query("select * from users where id = " . $id);  

... processing variables... load the finished content

在这里,我需要来自客户端的$id。可能吗?

javascript php jquery
3个回答
5
投票

是的..你可以通过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);  

0
投票

您可以使用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函数的输出,所以它取决于你想要在那里的信息。


0
投票

您可以使用Ajax将ID发布到您的PHP代码中。

$.ajax({
type: "POST",
url: "test.php",
data: { 'id': foo },
cache: false,
success: function(){
   alert("Order Submitted");
  }
});

PHP:

<?php
$id = $_POST['id']; 
© www.soinside.com 2019 - 2024. All rights reserved.