I'm trying to apply Ajax to my tbody
only not all my table
but I can't do this. The AJAX working good if I put my complete table in page. I want to split my table header & tbody
to be in separated page. The following code is AJAX:
function jsQueryData() {
var objData = {
action: actionSplit0 + '/QueryData',
};
$.post(
index_File,
objData,
function(data, status) {
$('#table-container').html(data)
},
'html'
)
}
This is the code of my table
. If I put the table code completely in one page the AJAX working good:
<table class="table table-striped table-bordered table-hover
main-table-h">
<thead>
<tr>
<td scope="col" class="search-th">
<input type="text" name="" value=""> <!-- for search -->
</td>
</tr>
<tr>
<th scope="col" class="align-top">item no</th>
</tr>
</thead>
<tbody>
<?php foreach ($allData1 as $data): ?>
<tr>
<td><?=$data->{2}; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I have tried to split the above code to be tbody
in separate page but the data didn't come as a table but all td
s come together.
You will need to return ajax results as json. I did not see your table and database structure so let me create a table as follows and insert values into it for purpose of testing
CREATE TABLE `users` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Insert Statetments for testing
INSERT INTO `users` (`id`, `username`, `name`, `email`, `timestamp`) VALUES
(1, 'nancy1', 'Nancy moore ', 'nancyfake@gmail.com', '2018-11-16 05:02:35'),
(2, 'tony1', 'tony moore ', 'tonyfake@gmail.com', '2018-11-16 05:08:23');
Here is a Re-write of your index.php reflecting ajax/jquery codes
<!doctype html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: 'result.php',
type: 'get',
dataType: 'JSON',
success: function(response){
var len = response.length;
for(var i=0; i<len; i++){
var id = response[i].id;
var username = response[i].username;
var name = response[i].name;
var email = response[i].email;
var tr_str = "<tr>" +
"<td align='center'>" + (i+1) + "</td>" +
"<td align='center'>" + username + "</td>" +
"<td align='center'>" + name + "</td>" +
"<td align='center'>" + email + "</td>" +
"</tr>";
$("#userTable tbody").append(tr_str);
}
}
});
});
</script>
</head>
<body>
<div class="container">
<table id="userTable" border="1" >
<thead>
<tr>
<th width="5%">S.no</th>
<th width="20%">Username</th>
<th width="20%">Name</th>
<th width="30%">Email</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</body>
</html>
config.php
Remember to edit database credentials to suit yours
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "your-db-name here"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
result.php
<?php
include "config.php";
$return_arr = array();
$query = "SELECT * FROM users ORDER BY NAME";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$username = $row['username'];
$name = $row['name'];
$email = $row['email'];
$return_arr[] = array("id" => $id,
"username" => $username,
"name" => $name,
"email" => $email);
}
// Encoding array in JSON format
echo json_encode($return_arr);
Thats all. Let me know how it goes
if i understand your problem, you want to set your thead static but your tbody by AJAX, if yep,
HTML:
<table class="table table-striped table-bordered table-hover
main-table-h">
<thead>
<tr>
<td scope="col" class="search-th">
<input type="text" name="" value=""> <!-- for search -->
</td>
</tr>
<tr>
<th scope="col" class="align-top">item no</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
JS (JQUERY):
$.ajax({
url: 'result.php',
type: 'get',
dataType: 'JSON',
success: function(response){
var tableData=$("table").ediTable({
json:{
body:response
},
editable:false // if you do not want it editable.
})
//and if you want to send JSON data of table you can :
jsonData=tableData.data();
// now you can send jsonData to PHP via ajax ...
}
});
Ref Lib:https://github.com/OxiGen1001/ediTable/
if i misunderstood your problem please let me know!