Azur Pazur Azur Pazur - 17 days ago 5
jQuery Question

why is ajax duplicating the whole page in php

I am connected to mysql database and I want to generate table with content from mysql with php after clicking on a button by a user.

But after clicking on a button, the whole page with header, body, etc. is generated to div where are table and php script. The button also duplicate visually of course.

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-2">
<meta http-equiv="content-language" content="cs">
<meta name="author" content="Marek Ciz, Tomas Veskrna">
<meta name="keywords" content="galerie, iis, iis projekt 2016, informacni system">
<link rel="icon" type="image/png" href="./icons/gallery.png" />
<title>Employee</title>
<link rel="stylesheet" type="text/css" href="./mystyle.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
$("#expo-but").click(function(){

$.ajax({
url: "./employee.php",
type: "post",
data: {action: "exposition"},
success: function(result) {
$("#table").html(result);
}});
});
});
</script>

</head>
<body>

<div class="page">
<div class="menu">
<button id="expo-but">Exposition</button>
</div>
<div id="table-wrapper">
<div id="table">
<table class="striped">
<thead>
<tr class="header">
<td>Id</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<?php
include './db_init.php';

//echo $_SESSION["user"];

if(isset($_POST['action'])){
if($_POST['action'] == "exposition") {
$sql = "SELECT id_zamestnance, jmeno FROM Zamestnanec";
$result = mysql_query($sql)or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row[id_zamestnance]."</td>";
echo "<td>".$row[jmeno]."</td>";
}
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>


Could anyone help me with this?
Thank you!

Answer

cut this code and add this code to top of the page

<?php
                include './db_init.php';

                //echo $_SESSION["user"];

                if(isset($_POST['action'])){   
                    if($_POST['action'] == "exposition") {
                        $sql = "SELECT  id_zamestnance, jmeno FROM Zamestnanec";
                        $result = mysql_query($sql)or die(mysql_error());

                        while ($row = mysql_fetch_assoc($result)) {
                            echo "<tr>";
                            echo "<td>".$row[id_zamestnance]."</td>";
                            echo "<td>".$row[jmeno]."</td>";                                                                    
                        }                           
                    }  
 exit();                         
                }
            ?>
Comments