1

I develop a chat widget using php (chat.php) and want to add it into another php page (blank.php). chat.php requires script that I put in Javascript files outside.

So in blank.php, I included js files that chat.php needed.

<!DOCTYPE html>
<html lang="en">
<head>
  <!--code-->
</head>

<body>
  <div id='includedContent'></div>

<!--dependency for chat.php-->
<link type="text/css" href="http://localhost/chatbox/gplus/chat.css" rel="stylesheet" />
<link type="text/css" href="http://localhost/chatbox/gplus/bootstrap.min.css" rel="stylesheet" />
<script type="text/javascript" src="http://localhost/chatbox/gplus/bootstrap.min.js"></script>
<script type="text/javascript" src="http://localhost/chatbox/gplus/jquery.min.js"></script>
<script type="text/javascript" src="http://localhost/chatbox/gplus/chat.js"></script>
<script type="text/javascript" src="http://localhost/chatbox/gplus/moment.min.js"></script>
<!--end-->

<!--include chat.php-->
<script>
    $("#includedContent").load("http://localhost/chatbox/gplus/chat.php");
</script>
<!--end-->

</body>
</html>

chat.php is loaded but the Javascript does not load for chat.php.

I also tried to use this and it works

<!--include chat.php-->
<?php
  $chat = file_get_contents('http://localhost/chatbox/gplus/chat.php');
  echo $chat;
  //or by using - include './chat.php';
?>
<!--end-->

But I wonder if there is a solution using javascript so I can include chat.php into blank.php, and chat.php can execute the js script included in blank.php. I put the script in blank.php to avoid script conflict between widget and hosting page.

Thank you.

1 Answer 1

1

I think what you need to use is include statement in PHP. See this LINK.

Basic example on how to use:

For vars.php:

 <?php
 $color = 'green';
 $fruit = 'apple';
 ?>

Included in test.php:

<?php
echo "A $color $fruit"; // A
include 'vars.php';
echo "A $color $fruit"; // A green apple
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I've tried that and it works. But i want to use javascript to include the other page.
If you use the include, you can execute javascript included from the page you are including.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.