Does external javascript call php for the widget?

I want users to be able to include my php file and load content from the file into some class.

For example, say the user has 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 to show the contents from myfile.php in .my-class :

<?php
$width = $_GET['width'];
$height = $_GET['height'];

echo "$('.my-class').load('load_content.php?width=$width&height=$height')";
?>

and into load_content.phpsomething simple:

<?php 
echo $_GET['width'] + $_GET['height'];
?>

I cannot find a similar question anywhere, I tried something, but only that I was able to do document.write something on user.html from myfile.php, but when I tried to load something or use innerHTML I get a blank page.

+4
source share
1 answer

, ajax <script>. , 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>
Hide result
0

Source: https://habr.com/ru/post/1624007/


All Articles