Well I'm learning to program php and I have a lot of difficulties in doing the junction of it with other language, at that moment I'm doing a job and I have a problem:
When I click on a div
the image on it goes to another, on the side of the site with .appendTo()
, so far so good. But I need whenever a picture passes to this location call a function php where I get information from a database. I was trying to do this by calling the ajax within the function that copies the Image. But then I can not assign anything to $_POST
within the php
JavaScript function in the file script.js
var idRecebido;
function CopiarDiv(x){
if(document.getElementById("copia").children.length <1){
$('#img'+x).appendTo('#copia');
idRecebido=x;
}
else{
$('#img'+idRecebido).appendTo('#div'+idRecebido);
$('#img'+x).appendTo('#copia');
idRecebido=x;
}
$.ajax({
type: "POST",
url: "index.php",
data: {id:x}
});
};
PHP page index.php
<html>
<head>
<meta charset="UTF-8">
<link rel='stylesheet' type="text/css" href='estilo.css'>
<script type='text/javascript' src='script.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><title>ProjetoFinal</title></head><body><?phpinclude'Class.php';echo'<divclass="SUPREMA">';
for($i=1; $i<=6; $i++){
//Divs da Esquerda
echo "<div class='alo' id=div".$i." onclick='CopiarDiv(".$i."); '>";
echo "<img src='passaros/bird_".$i.".png' style='width:250px; height:250px;' id=img".$i.">";
echo "</div>";
}
echo '</div>';
echo'<div class="info">';
echo '<div class="alo2" id="copia">';
echo '</div>';
echo '</div>';
?>
</body>
</html>
PHP function that takes the information in the database in the file Class.php
:
<?php
class Funções{
function pegarInfo($id){
$msqlConn= mysqli_connect('localhost', 'root', 'root', 'ProjetoFinal');
$show=mysqli_query($msqlConn,"select nome as 'Nome', nome_c as 'Nome Cientifíco', habitat as 'Habitat', tamanho as 'Tamanho (CM)' from passaros where id=".$id);
while($coll=mysqli_fetch_row($show)){
for($i=0;$i<mysqli_num_fields($show);$i++){
echo $coll[$i]." ";
echo "\n";
}
}
mysqli_close($msqlConn);
}
}
?>
Does anyone know how I can do this?