1
Well I’m learning to program php and I have a lot of difficulties in making the junction of it with other language, right now I’m doing a job and I have a problem:
When I click on one div
the image on it goes to another, on the side of the site with the .appendTo()
, so far so good. But I need that whenever an image 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 inside the function that copies the image. But then I can’t 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>Projeto Final</title>
</head>
<body>
<?php
include 'Class.php';
echo '<div class="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 picks up 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 it?
You should not use accentuation, for example Functions should turn Functions.
– Wictor Chaves
Cara explains something to me that you are concatenating variable and you are already inside php 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'>";
– Tulio Vieira
Wictor Keys I also thought about it but the problem is not this because I can call normally Functions, the problem is the fact that to use the way I want I have to take the value of idReceived to use as parameter and I do not know how to do it
– Vinicius Braga
Tulio I did this way because what generates the first Ivs with the images is a for and this way I can take the images by the name pattern and put the parameters right in the copy() function, it was one of the ways I thought and the one that worked most :p
– Vinicius Braga