Sainath Krishnan
Sainath Krishnan

Reputation: 2139

Loading div content from external file

I have a frontend.php with the following code :

<html>
<head>
</head>
<body>
<div id="someid" style="padding:10px; background-color:#CCCCCC">
<script type="text/javascript" src="http://www.example.com/backend.php?id=123"></script>
</div>
</body>
</html>

This is to access backend.php (shown below) and retrieve some data from the database, and display it in the div on the frontend. However, instead it displays the data on the bottom of the page. Is there any way to specifically load the content into the div where the script is put at frontend.php ?

var link='<a href="http://example2.com/code>id=<?php echo $id?>"><img src="https://www.example.com/img/datagraph.jpg" alt="Data" width="150" height="125" border="0"></a><br/><br/>';
window.getElementById("someid").innerHTML += link;

Upvotes: 1

Views: 582

Answers (2)

bohrsty
bohrsty

Reputation: 426

If you 'll give a chance to the jquery library, it offers a function load() that loads the given URI and inserts the result directly into the HTML element.

<div id="someId"></div>
<script type="text/javascript">
    $("#someId").load("http://www.example.com/backend.php?id=123");
</script>

Upvotes: 0

Oleg Dubas
Oleg Dubas

Reputation: 2348

You should give the <div> an ID:

<div id="mydiv"></div>

And then:

window.getElementById("mydiv").innerHTML += link;

Upvotes: 4

Related Questions