I know you can't use PHP inside of Javascript. Basically what I'm trying to do Is after clicking a link on one side of the web page, information that was stored in a database will appear on the other side. So I know I can use javascript to make my lists on one side clickable but I also know that to get the data I want displayed from my database I need to use PHP to get it. Is there a way to make this work?
EDIT: Heres some of my current code At what point could I implement PHP here after the user clicks on one of the lists?
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
function myFnc(x) {
if (x == 'Tiger') {
document.getElementById('imgDiv').innerHTML = 'Img1';
} else if (x == 'Bull') {
document.getElementById('imgDiv').innerHTML = 'Img3';
}
}
function myFunc2(x) {
if (x == 'Hammer') {
document.getElementById('imgDiv').innerHTML = 'Img2';
}
}
</script>
<article>
<ul style="list-style-type:none">
<li><a href="#Tiger" onclick="myFnc('Tiger')">Tiger</a></li>
<li><a href="#Hammerhead" onclick="myFunc2('Hammer')">Hammerhead</a></li>
<li><a href="#Bull" onclick="myFnc('Bull')">Bull</a></li>
<li><a href="#Great White">Great White</a></li>
<li><a href="#Mako">Mako</a></li>
<li><a href="#Greenland">Greenland</a></li>
<li><a href="#Whale">Whale</a></li>
<li><a href="#Thresher">Thresher</a></li>
<li><a href="#Oceanic">Oceanic WhiteTip</a></li>
<li><a href="#Goblin">Goblin</a></li>
</ul>
</article>
<div class="hold">
<div id="imgDiv"></div>
</div>
Write like this
on the First side the link should be:
'other_side.php?show_table=true'
And on the other side the site should be something like this
<?php
if(isset($_GET['show_table'])) {
// connect to database and do database query here
}
?>
<!Doctype html>
<html>
</html>
| id | product | price |
|----|-----------|--------|
| 1 | Cheese | 10 |
| 2 | Milk | 20 |
| 3 | Candy | 5 |
[
{id: 1, product: "Cheese", price: 10},
{id: 2, product: "Milk", price: 20},
{id: 3, product: "Candy", price: 5}
]
Third, once you have these done, use an AJAX call to retrieve the data from the get_data.php, you can find a lot of examples with Jquery and Vanilla JS.
Finally, just use a click event on the link.
Here you can find an example of every separated task:
<?php
$data = /** The data from our table **/;
header('Content-Type:application/json');
echo json_encode($data);
?>;