简体   繁体   中英

External javascript call php for widget?

I want that users can include my php file and load content from file in some class.

For example, let say that user have this file: 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:

<?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:

<?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.

I think you are trying to do an ajax call using a <script> tag. Here's what you should rather do, since you have jquery included on your page already:

 <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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM