My goal is to get via ajax the return of a php file. I have the following HTML form, I put the image to not break the indentation of the code.
My function:
<script type="text/javascript">
function pedir(){
$.ajax({
method: "post",
url: "atualiza.estoque.php",
data: $("#form").serialize(),
success: function(data){
alert("Busca efetuado com sucesso !");
}
});
}
</script>
My PHP:
<?php
include "../php/bddata.php";
header('Content-type: application/json; charset=utf-8');
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql="SELECT * FROM produto";
$result=$conn->query($sql);
while($row=$result->fetch_assoc()){
echo "id: " . $row["idProduto"]. " - Name: " . $row["nome"];
}
echo json_encode(['success' => true]);
$conn->close();
?>
I can not see what's wrong with this code, I'd be grateful if anyone could help me. Sorry for my English, I'm not native.