[英]External javascript call php for widget?
I want that users can include my php file and load content from file in some class. 我希望用户可以包含我的php文件并从某个类的文件中加载内容。
For example, let say that user have this file: user.html 例如,假设用户拥有此文件: 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>
and i want from myfile.php in .my-class show some content: 我希望从.my-class中的 myfile.php中显示一些内容:
<?php
$width = $_GET['width'];
$height = $_GET['height'];
echo "$('.my-class').load('load_content.php?width=$width&height=$height')";
?>
and in load_content.php
something simple for now: 并且在
load_content.php
中现在很简单:
<?php
echo $_GET['width'] + $_GET['height'];
?>
I can't find similar question anywhere, i tried something but only what i success is to document.write something on user.html from myfile.php, but when i tried load something or use innerHTML i get blank page. 我无法在任何地方找到类似的问题,我尝试了一些东西,但只有我成功的是document.write user.html来自myfile.php的东西,但当我尝试加载东西或使用innerHTML我得到空白页。
I think you are trying to do an ajax call using a <script>
tag. 我想你正在尝试使用
<script>
标签进行ajax调用。 Here's what you should rather do, since you have jquery included on your page already: 这是你应该做的事情,因为你的页面上已经包含了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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.