如何使用 Jquery 包含 php 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9652328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:22:31  来源:igfitidea点击:

How to include php file using Jquery?

phpjqueryinclude

提问by Jordan

I have that div:

我有那个 div:

<div id="content"></div>

and I want to include inside PHP file using jQuery.

我想使用 jQuery 包含在 PHP 文件中。

I try with this, but doesn't work:

我尝试这样做,但不起作用:

var about_me = "<?php include('php/about_me.php') ?>"

$('$content').click(function(){
 $('#content').text(about_me);
});

This returns me PHP code like a string?

这会像字符串一样返回我的 PHP 代码?

回答by xdazz

You could use .load:

你可以使用.load

$("#content").load('/about_me.php');

回答by StackOverflowed

Is it a .php file? IF it's a .js or .html file, Apache (or whatever webserver you're using) won't interpret it as a PHP file and won't include the relevant file.

它是一个 .php 文件吗?如果它是 .js 或 .html 文件,Apache(或您使用的任何网络服务器)不会将其解释为 PHP 文件,也不会包含相关文件。

回答by Deepak Soni

this is a simple way that you can do it. Just do it.

这是一个简单的方法,你可以做到。去做就对了。

just put the file name in .loadfunction

只需将文件名放在.load函数中

$(function(){  $("#includedContent").load("header.php"); });