我希望用户可以包含我的 php 文件并从某个类中的文件加载内容。
例如,假设用户有这个文件: user.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="myfile.php?width=100&height=100">
</script>
</head>
<body>
<div class="my-class">
</div>
</body>
</html>
我想从 .my-class 中的 myfile.php 显示一些内容:
<?php
$width = $_GET['width'];
$height = $_GET['height'];
echo "$('.my-class').load('load_content.php?width=$width&height=$height')";
?>
在 load_content.php
中,现在做一些简单的事情:
<?php
echo $_GET['width'] + $_GET['height'];
?>
我在任何地方都找不到类似的问题,我尝试了一些但唯一成功的是从 myfile.php 在 user.html 上记录.write 一些东西,但是当我尝试加载一些东西或使用 innerHTML 我得到空白页。
最佳答案
我认为您正在尝试使用 <script>
进行 ajax 调用标签。这是您应该做的,因为您的页面中已经包含了 jquery:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div class="my-class">
</div>
<script>
$(document).ready(function(){
$.get('load_content.php?width=100&height=100')
.done(function(resp){
$('.my-class').html(resp);
})
.fail(function(){
$('.my-class').html('<h2>There was an error loading content</h2>');
});
});
</script>
</body>
</html>
关于javascript - 外部 javascript 为小部件调用 php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34754551/