<select name="user_status" id="user_status">
<option value="">--User Status--</option>
<option value="1">Active Users</option>
<option value="0">Inactive Users</option>
</select>
AJAX:var myTable =
$('#lockUsersTable').DataTable({
"pageLength": 25,
select: {style: 'multi'},
columns: [
{ data: 'count' },
{ data: 'initials' },
{ data: 'username' },
{ data: 'user_type' },
{ data: 'status', orderable: false },
{ data: 'lock', orderable: false }
]
});
$('#user_status').on('change', function() {
$('#tableResponse').html('<i class="ace-icon fa fa-spinner fa-spin blue bigger-115"></i> Please wait...');
$.ajax({
method : 'post',
url : '../../lockUsers_Data',
data : { userStatus : $('#user_status').val() },
dataType : 'json',
}).done(function(data) {
myTable.clear().draw();
myTable.rows.add(data).draw();
var statusVal = $('#user_status').val();
if ( myTable.data().any() ) {
if ( statusVal == 1 ){
$('#tableResponse').html("Active Users");
}else if(statusVal == 0){
$('#tableResponse').html("Inactive Users");
}else{
$('#tableResponse').html("Select Users' Status")
}
}
else{
if ( statusVal == 1 ){
$('#tableResponse').html("No active users found");
}else if(statusVal == 0){
$('#tableResponse').html("No inactive users found");
}else{
$('#tableResponse').html("Select Users' Status");
}
}
});
});
The PHP script:<?php
include('../../db_connection.php');
//$user_status = 1;
$stmt = $conn->prepare( "SELECT UserId, Initials, Username, Status, User_Type FROM table WHERE Status = ? ORDER BY User_Type ASC" );
$stmt->bind_param( "i", $_POST['userStatus'] );
$stmt->execute();
$users = $stmt->get_result();
$data = array();
$count = 1;
while ( $user = $users->fetch_object() ):
if ( $user->Status == 1 ) {
$status = "<span class='badge badge-sm badge-success'>Active</span> ";
} else{
$status = "<span class='badge badge-sm badge-danger'>Inactive</span> ";
}
$lock = "<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[".$user->UserId."]' value=".$user->UserId." ><span class='lbl'></span></label></div>";
$data[] = array(
'count' => $count++,
'user_id' => $user->UserId,
'initials' => $user->Initials,
'username' => $user->Username,
'user_type' => $user->User_Type,
'status' => $status,
'lock' => $lock
);
endwhile;
$response = array(
'data' => $data,
);
die( json_encode($response) );
Strict standards: Only variables should be passed by reference in C:\..\..\lockUsers_Data.php on line 6
So, I stored the value in a variable this way:$user_status = intval($_POST['userStatus']);
and pass it this way:$stmt->bind_param( "i", $user_status );
Yet, no luck. Same as before
echo $_POST['userStatus'];
var user_status = $(this).val();
alert(user_status);
$.ajax({
method : 'post',
url : '../../lockUsers_Data',
data : { userStatus : user_status },
dataType : 'json',
...
echo $_POST['userStatus'];I can't see anything. I added it on top of the PHP script this way:
<?php
include('../../db_connection.php');
echo $_POST['userStatus'];
Undefined index: userStatus in...
echo "===================".$_POST['userStatus']."===================";
===================1===================
var user_status = $(this).val();
alert(user_status);
returned 1 and 0 for selected option. The values seem correct
if ($stmt->execute()) {
echo '=========it worked=========';
} else {
echo '========='.$stmt->error.'=========';
}
===================1============================it worked========={,…}
echo '========='.$users->num_rows.'==========';
===================1============================it worked==================17==========
Do you get this as a response when you're changing the select value from the list?YES.
data: [{count: 1, user_id: 49, initials: "HARDSOFT APP", username: "admin", user_type: "staff",…},…]
0: {count: 1, user_id: 49, initials: "HARDSOFT APP", username: "admin", user_type: "staff",…}
1: {count: 2, user_id: 52, initials: "DEMO FORM MASTER", username: "fm", user_type: "staff",…}
2: {count: 3, user_id: 53, initials: "DEMO STAFF", username: "staff", user_type: "staff",…}
3: {count: 4, user_id: 60, initials: "ABDULRASHEED G. O.", username: "orpee", user_type: "staff",…}
4: {count: 5, user_id: 81, initials: "SUNDAY STAFF", username: "sunday", user_type: "staff",…}
5: {count: 6, user_id: 95, initials: "MONDAY STAFF", username: "monday", user_type: "staff",…}
6: {count: 7, user_id: 126, initials: "JOHN DOE", username: "doe", user_type: "staff",…}
7: {count: 8, user_id: 129, initials: "GOODNESS", username: "ness", user_type: "staff",…}
8: {count: 9, user_id: 134, initials: "RABI ABDULRASHEED", username: "rabi", user_type: "staff",…}
9: {count: 10, user_id: 135, initials: "OWOLABI ALAMU", username: "alamu", user_type: "staff",…}
10: {count: 11, user_id: 62, initials: "DEMO STUDENT 1", username: "0001", user_type: "student",…}
11: {count: 12, user_id: 70, initials: "ADEBISI ADEKUNLE GOLD", username: "GSS/2222", user_type: "student",…}
12: {count: 13, user_id: 71, initials: "RABIAT ALAMU", username: "GSS/0011", user_type: "student",…}
13: {count: 14, user_id: 73, initials: "DEMO 4", username: "4", user_type: "student",…}
14: {count: 15, user_id: 78, initials: "DHDHDH", username: "GSS/00016", user_type: "student",…}
15: {count: 16, user_id: 136, initials: "OWOLABI ALAMU", username: "Gss/0009", user_type: "student",…}
16: {count: 17, user_id: 138, initials: "BEST TEACHER", username: "3214", user_type: "student",…}
OK, I see, what is the main difference between the data you get when you change the value from the select list and the data you get when you hardcode the 1 value?Here, same
data: [{count: 1, user_id: 49, initials: "HARDSOFT APP", username: "admin", user_type: "staff",…},…]
0: {count: 1, user_id: 49, initials: "HARDSOFT APP", username: "admin", user_type: "staff",…}
1: {count: 2, user_id: 52, initials: "DEMO FORM MASTER", username: "fm", user_type: "staff",…}
2: {count: 3, user_id: 53, initials: "DEMO STAFF", username: "staff", user_type: "staff",…}
3: {count: 4, user_id: 60, initials: "ABDULRASHEED G. O.", username: "orpee", user_type: "staff",…}
4: {count: 5, user_id: 81, initials: "SUNDAY STAFF", username: "sunday", user_type: "staff",…}
5: {count: 6, user_id: 95, initials: "MONDAY STAFF", username: "monday", user_type: "staff",…}
6: {count: 7, user_id: 126, initials: "JOHN DOE", username: "doe", user_type: "staff",…}
7: {count: 8, user_id: 129, initials: "GOODNESS", username: "ness", user_type: "staff",…}
8: {count: 9, user_id: 134, initials: "RABI ABDULRASHEED", username: "rabi", user_type: "staff",…}
9: {count: 10, user_id: 135, initials: "OWOLABI ALAMU", username: "alamu", user_type: "staff",…}
10: {count: 11, user_id: 62, initials: "DEMO STUDENT 1", username: "0001", user_type: "student",…}
11: {count: 12, user_id: 70, initials: "ADEBISI ADEKUNLE GOLD", username: "GSS/2222", user_type: "student",…}
12: {count: 13, user_id: 71, initials: "RABIAT ALAMU", username: "GSS/0011", user_type: "student",…}
13: {count: 14, user_id: 73, initials: "DEMO 4", username: "4", user_type: "student",…}
14: {count: 15, user_id: 78, initials: "DHDHDH", username: "GSS/00016", user_type: "student",…}
15: {count: 16, user_id: 136, initials: "OWOLABI ALAMU", username: "Gss/0009", user_type: "student",…}
16: {count: 17, user_id: 138, initials: "BEST TEACHER", username: "3214", user_type: "student",…}
when I used hard-coded value (1 or 0) in my PHP script and request AJAX call when page loads NOT onchange
var myTable =
$('#lockUsersTable').DataTable({
"pageLength": 25,
ajax : '../../lockUsers_Data',
select: {style: 'multi'},
columns: [
{ data: 'count' },
{ data: 'initials' },
{ data: 'username' },
{ data: 'user_type' },
{ data: 'status', orderable: false },
{ data: 'lock', orderable: false }
]
});
var myTable = $('#lockUsersTable').DataTable({
"pageLength": 25,
"ajax": {
"url": '../../lockUsers_Data',
"type": "POST",
"data": { userStatus : 1 }
},
select: {style: 'multi'},
columns: [
{ data: 'count' },
{ data: 'initials' },
{ data: 'username' },
{ data: 'user_type' },
{ data: 'status', orderable: false },
{ data: 'lock', orderable: false }
]
});
}).done(function(data) {
var myTable = $('#lockUsersTable').DataTable();
myTable.clear();
myTable.rows.add(data);
myTable.draw();
});
}).done(function(data) {
var myTable = $('#lockUsersTable').DataTable({
"pageLength": 25,
select: {style: 'multi'},
columns: [
{ data: 'count' },
{ data: 'initials' },
{ data: 'username' },
{ data: 'user_type' },
]
});
myTable.clear();
myTable.rows.add([
{count: 1, user_id: 49, initials: "HARDSOFT APP", username: "admin", user_type: "staff"},
{count: 2, user_id: 52, initials: "DEMO FORM MASTER", username: "fm", user_type: "staff"},
{count: 3, user_id: 53, initials: "DEMO STAFF", username: "staff", user_type: "staff"},
{count: 4, user_id: 60, initials: "ABDULRASHEED G. O.", username: "orpee", user_type: "staff"},
{count: 5, user_id: 81, initials: "SUNDAY STAFF", username: "sunday", user_type: "staff"},
{count: 6, user_id: 95, initials: "MONDAY STAFF", username: "monday", user_type: "staff"},
{count: 7, user_id: 126, initials: "JOHN DOE", username: "doe", user_type: "staff"},
]);
myTable.draw();
});
var myTable = $('#lockUsersTable').DataTable({
"pageLength": 25,
"ajax": {
"url": '../../lockUsers_Data',
"type": "POST",
"data": { userStatus : 1 }
},
select: {style: 'multi'},
columns: [
{ data: 'count' },
{ data: 'initials' },
{ data: 'username' },
{ data: 'user_type' },
{ data: 'status', orderable: false },
{ data: 'lock', orderable: false }
]
});
Actually populated the table. I'll try the call back you asked me to try now...
}).done(function(data) {
var myTable = $('#lockUsersTable').DataTable();
myTable.clear();
myTable.rows.add(data);
myTable.draw();
});
}).done(function(data) {
var myTable = $('#lockUsersTable').DataTable({
"pageLength": 25,
select: {style: 'multi'},
columns: [
{ data: 'count' },
{ data: 'initials' },
{ data: 'username' },
{ data: 'user_type' },
]
});
myTable.clear();
myTable.rows.add([
{count: 1, user_id: 49, initials: "HARDSOFT APP", username: "admin", user_type: "staff"},
{count: 2, user_id: 52, initials: "DEMO FORM MASTER", username: "fm", user_type: "staff"},
{count: 3, user_id: 53, initials: "DEMO STAFF", username: "staff", user_type: "staff"},
{count: 4, user_id: 60, initials: "ABDULRASHEED G. O.", username: "orpee", user_type: "staff"},
{count: 5, user_id: 81, initials: "SUNDAY STAFF", username: "sunday", user_type: "staff"},
{count: 6, user_id: 95, initials: "MONDAY STAFF", username: "monday", user_type: "staff"},
{count: 7, user_id: 126, initials: "JOHN DOE", username: "doe", user_type: "staff"},
]);
myTable.draw();
});
<?php
sleep(1);
require_once '../../connection.php';
$stmt = $conn->prepare( "SELECT UserId, Initials, Username, Status, User_Type FROM tableWHERE Status = ? ORDER BY User_Type ASC" );
$stmt->bind_param( "i", $_POST['userStatus'] );
$stmt->execute();
$students = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
die( json_encode($students) );
?>
And AJAX:var myTable =
$('#lockUsersTable').DataTable({
"pageLength": 25,
//ajax : '../data/lockUsers_Data',
select: {style: 'multi'},
columns: [
{ data: 'Initials' },
{ data: 'Username' },
{ data: 'User_Type' },
{
"orderable": false,
render: function ( data, type, row ) {
return '<div class="center"><label class="pos-rel"><input type="checkbox" class="ace" name="userSelect[' + row.UserId + ']" value="' + row.UserId + '"><span class="lbl"></span></label></div>';
}
}
]
});
I don't even know how to do auto-numbering column using this method.}).done(function(data) {
var myTable = $('#lockUsersTable').DataTable({
"pageLength": 25,
select: {style: 'multi'},
columns: [
{ data: 'count' },
{ data: 'initials' },
{ data: 'username' },
{ data: 'user_type' },
]
});
myTable.clear();
myTable.rows.add(data);
myTable.draw();
});
myTable.rows.add([
{count: 1, user_id: 49, initials: "HARDSOFT APP", username: "admin", user_type: "staff"},
{count: 2, user_id: 52, initials: "DEMO FORM MASTER", username: "fm", user_type: "staff"},
{count: 3, user_id: 53, initials: "DEMO STAFF", username: "staff", user_type: "staff"},
{count: 4, user_id: 60, initials: "ABDULRASHEED G. O.", username: "orpee", user_type: "staff"},
{count: 5, user_id: 81, initials: "SUNDAY STAFF", username: "sunday", user_type: "staff"},
{count: 6, user_id: 95, initials: "MONDAY STAFF", username: "monday", user_type: "staff"},
{count: 7, user_id: 126, initials: "JOHN DOE", username: "doe", user_type: "staff"},
]);
Have you tried the code from my last comment AbdulRasheed?Yes, I have tried it. Same thing
{"data":[{"count":1,"user_id":49,"initials":"HARDSOFT APP","username":"admin","user_type":"staff","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[49]' value=49 ><span class='lbl'><\/span><\/label><\/div>"},{"count":2,"user_id":52,"initials":"DEMO FORM MASTER","username":"fm","user_type":"staff","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[52]' value=52 ><span class='lbl'><\/span><\/label><\/div>"},{"count":3,"user_id":53,"initials":"DEMO STAFF","username":"staff","user_type":"staff","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[53]' value=53 ><span class='lbl'><\/span><\/label><\/div>"},{"count":4,"user_id":60,"initials":"ABDULRASHEED G. O.","username":"orpee","user_type":"staff","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[60]' value=60 ><span class='lbl'><\/span><\/label><\/div>"},{"count":5,"user_id":81,"initials":"SUNDAY STAFF","username":"sunday","user_type":"staff","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[81]' value=81 ><span class='lbl'><\/span><\/label><\/div>"},{"count":6,"user_id":95,"initials":"MONDAY STAFF","username":"monday","user_type":"staff","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[95]' value=95 ><span class='lbl'><\/span><\/label><\/div>"},{"count":7,"user_id":126,"initials":"JOHN DOE","username":"doe","user_type":"staff","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[126]' value=126 ><span class='lbl'><\/span><\/label><\/div>"},{"count":8,"user_id":129,"initials":"GOODNESS","username":"ness","user_type":"staff","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[129]' value=129 ><span class='lbl'><\/span><\/label><\/div>"},{"count":9,"user_id":134,"initials":"RABI ABDULRASHEED","username":"rabi","user_type":"staff","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[134]' value=134 ><span class='lbl'><\/span><\/label><\/div>"},{"count":10,"user_id":135,"initials":"OWOLABI ALAMU","username":"alamu","user_type":"staff","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[135]' value=135 ><span class='lbl'><\/span><\/label><\/div>"},{"count":11,"user_id":62,"initials":"DEMO STUDENT 1","username":"0001","user_type":"student","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[62]' value=62 ><span class='lbl'><\/span><\/label><\/div>"},{"count":12,"user_id":70,"initials":"ADEBISI ADEKUNLE GOLD","username":"GSS\/2222","user_type":"student","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[70]' value=70 ><span class='lbl'><\/span><\/label><\/div>"},{"count":13,"user_id":71,"initials":"RABIAT ALAMU","username":"GSS\/0011","user_type":"student","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[71]' value=71 ><span class='lbl'><\/span><\/label><\/div>"},{"count":14,"user_id":73,"initials":"DEMO 4","username":"4","user_type":"student","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[73]' value=73 ><span class='lbl'><\/span><\/label><\/div>"},{"count":15,"user_id":78,"initials":"DHDHDH","username":"GSS\/00016","user_type":"student","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[78]' value=78 ><span class='lbl'><\/span><\/label><\/div>"},{"count":16,"user_id":136,"initials":"OWOLABI ALAMU","username":"Gss\/0009","user_type":"student","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[136]' value=136 ><span class='lbl'><\/span><\/label><\/div>"},{"count":17,"user_id":138,"initials":"BEST TEACHER","username":"3214","user_type":"student","status":"<span class='badge badge-sm badge-success'>Active<\/span> ","lock":"<div class='center'><label class='pos-rel'><input type='checkbox' class='ace' name='userSelect[138]' value=138 ><span class='lbl'><\/span><\/label><\/div>"}]}
Have you tried to parse the coming value to an integer using `intval` like :
Open in new window
Since your bind_param wait for the corresponding variable to have an integer type and the .val() in the JS part return always a string that may the part that creates the problem.