I have an img that I added to the page using php - from DB. I would like to change it to another img from the DB based on 'onclick' meaning I would like to change the img in the tag I created before.
Here is the code of the object I would like to change
<?php
include("dbAccess.php" );
$sql = "SELECT * FROM tbl_bigImg_202 where id=6"; //query string
$result = $connection -> query($sql);
if ($result -> num_rows > 0) {
// output data of each row
while($row = $result -> fetch_assoc()){
echo '<img id="mainPants" src="' . $row['img'] . '"/>';
}
}
?>
Here are the img I would like to click that it would change
<?php
$sql = "SELECT * FROM tbl_smallImg_202"; //query string
$result = $connection -> query($sql);
if ($result -> num_rows > 0) {
// output data of each row
while($row = $result -> fetch_assoc()){
if ($row['id'] == 5){
echo '<img id="pBlack" onclick="changeImage(this)" src="' . $row['img'] . '"/>';
}
if ($row['id'] == 7){
echo '<img id="pPink" onclick="changeImage(this)" src="' . $row['img'] . '"/>';
}
if ($row['id'] == 8){
echo '<img id="pRed" onclick="changeImage(this)" src="' . $row['img'] . '"/>';
}
}
}
?>
And here is what I tried to do
<script>
function changeImage(img) {
var newImg = document.getElementById('mainPants');
<?php
$sql = "SELECT * FROM tbl_bigImg_202"; //query string
$result = $connection -> query($sql);
if ($result -> num_rows > 0) {
// output data of each row
while($row = $result -> fetch_assoc()){
switch (img.id) {
case 'pBlack':
{
if($row['id'] == 5);
newImg.src= $row['img'];
}
break;
case 'pPink':
{
if($row['id'] == 7);
newImg.src= $row['img'];
}
break;
case 'pRed':
default:
{
if($row['id'] == 8);
newImg.src= $row['img'];
}
}
}
}
mysqli_close($connection);
?>
}
</script>
How can I do that?