I'm trying to POST a variabile "id" to another php page by using Ajax.
In the second php page called "export.php" I need to use this variable to query a MySql table and display the results in a html table.
However it doen't work seems that on export.php the variable isn't set. Can you help me?
<script>
$('#btn_report').click(function(){
if(confirm("Are you sure?")) {
var id = [];
$(':checkbox:checked').each(function(i){
id[i] = $(this).val();
alert (id[i]); //this is correctly printed
});
if(id.length === 0) {
alert("No record selected");
}
else {
$.ajax({
url:'export.php',
type:'POST',
data:{id:id},
});
}
}
else {
return false;
}
});
</script>
export.php
So the problem is that the "if(isset..." return false and display the else alert on the bottom of this code.
<html lang="en">
<head>
<title>Export</title>
</head>
<body>
<?php
$connect = mysqli_connect('localhost', 'user', 'pass', 'db' );
if(isset($_POST["id"])){
echo 'break1';
foreach($_POST["id"] as $id) {
$sel = "SELECT * FROM `user_details` WHERE `Codice prodotto` = $id";
echo $sel . "<br>";
$res = mysqli_query($connect, $sel);
echo "<table border='1'>
<tr>
<th></th>
<th>Codice prodotto</th>
<th>Relazione tecnica</th>
<th>Modello offerto</th>
<th>Descrizione</th>
<th>Immagine</th>
<th>Tipologia di Ancoraggio</th>
<th>Design</th>
<th>Illuminazione</th>
<th>FSP</th>
<th>DATI TECNICI</th>
<th>Dissolvenza delle ombre</th>
</tr>";
while($row = mysqli_fetch_array($res))
{
echo '<tr id="' . $row['Codice prodotto'] . '">';
echo '<td><input type="checkbox" name="codice[]" class="delete_prodotto" value="' . $row['Codice prodotto'] . '"></td>';
echo "<td>" . $row['Codice prodotto'] . "</td>";
echo "<td>" . $row['Relazione tecnica'] . "</td>";
echo "<td>" . $row['Modello offerto'] . "</td>";
echo "<td>" . $row['Descrizione'] . "</td>";
echo "<td>" . $row['Immagine'] . "</td>";
echo "<td>" . $row['Tipologia di Ancoraggio'] . "</td>";
echo "<td>" . $row['Design'] . "</td>";
echo "<td>" . $row['Illuminazione'] . "</td>";
echo "<td>" . $row['FSP'] . "</td>";
echo "<td>" . $row['DATI TECNICI'] . "</td>";
echo "<td>" . $row['Dissolvenza delle ombre'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
}
else {
echo "no value";
}
?>
</body>
</html>