簡體   English   中英

外部JavaScript調用PHP的小部件?

[英]External javascript call php for widget?

我希望用戶可以包含我的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'];
?>

我無法在任何地方找到類似的問題,我嘗試了一些東西,但只有我成功的是document.write user.html來自myfile.php的東西,但當我嘗試加載東西或使用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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM