如何使用JavaScript包含PHP文件并传递参数

时间:2021-02-04 09:57:51

I need to run a PHP function in a page, after the user clicks on something, in this case, a link.

我需要在页面中运行一个PHP函数,在用户单击某个东西(在本例中是链接)之后。

First I was using onclick to run a javascript AJAX to request a PHP file with the function in it. The thing is, I need to pass a parameter to the function, and I can't seem to be able to do it with AJAX.

首先,我使用onclick运行一个javascript AJAX来请求一个包含函数的PHP文件。问题是,我需要向函数传递一个参数,而我似乎无法使用AJAX实现这一点。

If it was a PHP include, it would behave like it 'appended' the other file to the current file, so this way, the included file could reference variables in the current file. Apparently, AJAX is just taking the file I need, running it's code, and displaying the results. So yeah, doesn't work for me.

如果它是一个PHP,它的行为就像将另一个文件附加到当前文件,这样,包含的文件就可以引用当前文件中的变量。显然,AJAX只是获取我需要的文件,运行它的代码,并显示结果。对,对我没用。

So basically. I need an exact replica of PHP's 'include', on JavaScript. OR any other workaround.

所以基本上。我需要一个完全复制的PHP' include',在JavaScript上。或任何其他解决办法。

My current code:

我现在的代码:

<br> <div id="seeMore"> </div> <br>

<ul> <li style="float: left"> <a href="#" onclick="loadMore();">Ver Mais</a> </li> </ul>

<script type="text/javascript">
function loadMore()
{ $("#seeMore").load("feedP.php"); }
</script>

This is the part of the code in the main page that im needing help with, you can see it's loading a page called feedP.php, here's it's code:

这是主页中需要帮助的代码的一部分,您可以看到它正在加载一个名为feedP的页面。php,这是它的代码:

<?php

echo 'test';
require_once('functions.php'); loadFeeds($urls, 5);

?>

As you can see, I have to run a function called loadFeeds, and it requires a parameter, $urls. Witch is created back on the main page.

如您所见,我必须运行一个名为loadfeed的函数,它需要一个参数$urls。女巫是在主页上创建的。

EDIT:

编辑:

I CAN'T reload the page, or redirect the user, that's why i'm trying to 'append' the file here.

我不能重载页面,也不能重定向用户,这就是为什么我要在这里添加文件。

2 个解决方案

#1


2  

You can use the second parameter of the load function which takes parameters that can be posted to the file in question:

您可以使用load函数的第二个参数,该参数将获取可以发送到该文件的参数:

$("#seeMore").load("feedP.php", {
    url: 'url'
});

Then, in your PHP you can make use of $_POST to access the posted data.

然后,在PHP中,可以使用$_POST访问已发布的数据。

data

数据

Type: PlainObject or String

类型:PlainObject或字符串

A plain object or string that is sent to the server with the request.

与请求一起发送到服务器的普通对象或字符串。

Reading Material

阅读材料

.load

.load

#2


1  

You can pass the parameters through load function. I have altered your script code as below.

可以通过load函数传递参数。我已经修改了你的脚本代码如下。

<script type="text/javascript">
function loadMore() { 
   $("#seeMore").load("feedP.php", {urls: "<?php echo $url; ?>"}); 
}
</script>

In the above code $url is the php variable which you assign the url you need to pass to feedp.php

在上面的代码中,$url是一个php变量,您可以将需要传递给feedp.php的url分配给它。

#1


2  

You can use the second parameter of the load function which takes parameters that can be posted to the file in question:

您可以使用load函数的第二个参数,该参数将获取可以发送到该文件的参数:

$("#seeMore").load("feedP.php", {
    url: 'url'
});

Then, in your PHP you can make use of $_POST to access the posted data.

然后,在PHP中,可以使用$_POST访问已发布的数据。

data

数据

Type: PlainObject or String

类型:PlainObject或字符串

A plain object or string that is sent to the server with the request.

与请求一起发送到服务器的普通对象或字符串。

Reading Material

阅读材料

.load

.load

#2


1  

You can pass the parameters through load function. I have altered your script code as below.

可以通过load函数传递参数。我已经修改了你的脚本代码如下。

<script type="text/javascript">
function loadMore() { 
   $("#seeMore").load("feedP.php", {urls: "<?php echo $url; ?>"}); 
}
</script>

In the above code $url is the php variable which you assign the url you need to pass to feedp.php

在上面的代码中,$url是一个php变量,您可以将需要传递给feedp.php的url分配给它。