I'm new to dynamic website coding, I have a project that eventually will scale quite large.
I am unable to get my javascript to pull from my external website html files or from my external PHP file I've created that pulls content from a mysql table. I also don't quite know how to convert my static implementation into a dynamic one, I've provided details below on both the static version and the partial working/incomplete dynamic version below.
point form process:
- onclick javascript talks to php
- php talks to mysql accessing the content requested
- mysql sends back to php
- php sends to javascript
- javascript appends, prepends or loads(depending on what we want it to do) content into a specific DIV on webpage.
I had an example which somewhat worked for static html files:
HTML on the HTML page:
<a href="#" id="item1">item1</a>
DIV where content will load to:
<div id="content"></div>
javascript on the HTML page:
<script>
$('#item1').click(function() {
$.get("item1.html",function (test) { $("#content").prepend(test);});
});
</script>
This above example works for staticly prepending code without clearing the 'content' DIV each time you click the link which is what we want. However when trying to convert this static setup to a dynamic setup we are unable to do so.
I have also tried the following javascript alteration just to see if we could load html from an external source but it fails to do so(Not sure why my javascript wont load external pages?):
<script>
$('#item1').click(function() {
$.get("http://localhost/item1.html",function (test) { $("#content").prepend(test);});
});
</script>
What I believe would be the ultimate solution for a dynamic looking structure would be something like:
NOTE DOES NOT WORK, JUST CONCEPT
HTML would show:
<a href="#" id="I8">item8</a>
javascript concept:
- onclick of anchor link pass ID from the anchor to PageContent.php via PageContent.php?id=I8
- wait for PHP PageContent.php to process request
- php passes content back to javascript in the form of HTML and javascript then prepends/appends/loads the content to the #content DIV.
PHP PageContent.php INCOMPLETE:
<?php
$conn = mysqli_connect('localhost', 'user', 'pass', 'DB')
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$sql = "SELECT * FROM `content` where ID='<? ID VARIABLE that was passed from javascript HERE ?>';";
$result = mysqli_query($conn, $sql);
$resultarr = mysqli_fetch_assoc($result);
?>
I've never built php pages that you can pass information to and get a result from in this type of way where you have the '?ID='. How do you accomplish the passthrough of PageContent.php?ID=8
return $mysqlDataorecho $mysqlData