Datatables can not be responsive as example - javascript

i have a trouble with the datatables there.
I trying to add a datatables with responsive as the image
It's ok when i put it as a separate file but when i add the data from my database, i can not press on the plus icon to show detail about the member as the image
i just change the data and i don't know why it doesn't work.
Can you help?
This is my code: (i did close php tag for the first open php but i dont know why i can not post it on here)
<?php
$stt= 1;
$sql= "SELECT * from users ORDER BY level DESC";
//thuc hien cau lenh voi bien conn lay tu file connection.php
$query= mysqli_query($conn, $sql);
while ($data=mysqli_fetch_array($query)){
<tr>
<th scope="row"><?php echo $stt++?></th>
<td><?php echo $data["firstname"]?></td>
<td><?php echo $data["lastname"]?></td>
<td><?php echo $data["username"]?></td>
<td><?php echo $data["email"]?></td>
<td><?php echo $data["phone"]?></td>
<td>
<?php
if($data["level"] == 1){
echo "Administrator";
}else{
echo "Member";
}
?>
</td>
<td>
Detail
</td>
</tr>
<?php
}
?>

Please change your code
<th scope="row"><?php echo $stt++?></th>
to
<td scope="row"><?php echo $stt++?></td>
change th to td will fix your problem.

Related

Display alert on button submission

So I have built a very basic application system. I have made one page where I can see all the applications in a table via MySQL query. I have also made so I can click a button to either approve or deny the application. Dependning on which button I press, the row with that specific user will be moved do the approved or denied table in the database. But, I want to display an alert when pressing the different buttons at the same time they send the row id to the PHP code.
Here is my code for the application table site: (register.php)
<?php
$query = "SELECT * FROM ansokningar";
$query_run = mysqli_query($link, $query);
?>
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th> ID </th>
<th> Förnamn </th>
<th>Efternamn </th>
<th>Mejladress</th>
<th>Ålder</th>
<th>Ansökt som</th>
<th>Läs Ansökning</th>
<th>Godkänn</th>
<th>Neka</th>
</tr>
</thead>
<tbody>
<?php
if(mysqli_num_rows($query_run) > 0)
{
while($row = mysqli_fetch_assoc($query_run))
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fornamn']; ?></td>
<td><?php echo $row['efternamn']; ?></td>
<td><?php echo $row['mejladress']; ?></td>
<td><?php echo $row['alder']; ?></td>
<td><?php echo $row['intresseradavyrket']; ?></td>
<td>
<form action="register.php" method="post">
<input type="hidden" id="deleteid" name="delete" value="<?php echo $row['id']; ?>">
<center> <button id="delete" type="submit" name="delete" class="btn btn-danger btn-circle btn-sm">
<i class="fas fa-trash"></i></button></center>
</form>
</td>
</tr>
<?php
}
}
else {
echo $ingaansokning;
}
mysqli_close($link);
?>
And here is my PHP code for moving the row: (neka.php)
<?PHP require '../db/dbconfig.php';
if(isset($_POST['id']))
{
$id = $_POST['id'];
$query = "
INSERT INTO nekade SELECT * FROM ansokningar WHERE id='$id';
DELETE FROM ansokningar WHERE id='$id';
";
$query_run = $link->multi_query($query);
if($query_run)
{
$_SESSION['success'] = '';
header('Location: ../../register.php');
exit();
How do I send the row ID from the first page to the second and display one alert when it's done?
I have tried this on the first page (register.php) but it doesn't work:
<script>
$(function(){
$('#delete').click(function() {
var id = <?php echo $row['id']; ?>;
$.ajax({
type: 'POST',
url: 'includes/ansokningar/neka.php',
data:{id: id},
success: function(data){
Swal.fire(
'Grattis!',
'Din ansökan är nu skickad!',
'success'
)
}
})
})
});
(Without the ajax stuff it works to move the user to the other table)
Thanks!

How to print using iframe? ID does not get when print button is click

I want to print information that depends on the $stud_no using iframe. When I show the iframe in the table, it generates a correct $stud_no. But when I click the print button it only shows the First $stud_no. It's like the button didn't GET the id in admin_print-app-form-view.php
admin_print-app-form.php
<table id="dataTable2" class="text-center">
<thead class="text-capitalize">
<tr>
<th>NO.</th>
<th>LAST NAME</th>
<th>FIRST NAME</th>
<th>MIDDLE NAME</th>
<th>SEX</th>
<th>CONTACT NO.</th>
<th>ENTRY</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM stud_acc";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$iframeId = 'studframe' . $row['stud_no'];
$stud_no = $row['stud_no'];
$lastname = $row['lastname'];
$firstname = $row['firstname'];
$middlename = $row['middlename'];
$sex = $row['sex'];
$contact = $row['contact'];
$entry = $row['entry'];?>
<tr>
<td><?php echo $stud_no ?></td>
<td><?php echo $lastname ?></td>
<td><?php echo $firstname ?></td>
<td><?php echo $middlename ?></td>
<td><?php echo $sex ?></td>
<td><?php echo $contact ?></td>
<td><?php echo $entry ?></td>
<td>
<iframe src="admin_print-app-form-view.php?id=<?php echo "$stud_no"?>" name="frame" id="<?= $iframeId ?>" style="visibility:hidden;height:0px;width:0px"></iframe>
<button type="button" class="btn btn-roundedtb btn-info" onclick="document.getElementById('<?= $iframeId ?>').print();"><i class="fa fa-print"></i><span class="icon-name"> Print</span></button>
</td>
</tr>
} }?>
</tbody>
</table>
admin_print-app-form-view.php
<?php
session_start();
include("connection.php");
$stud_no = $_GET['id'];
?>
This is the print preview.The number in the Red circle should be the $stud_no that I clicked, but it always gives me first stud_no
The issue is that when you give the same name and id to every iframe, javascript will only give you the first match it finds, returning the same iframe for all buttons.
We need to give them unique id's (we can skip the name altogether).
Note: This code assumes that stud_no is a unique value, like the tables primary key.
In your while-loop, create a unique id:
while($row = $result->fetch_assoc()) {
// Create a unique id using the stud_no
$iframeId = 'studframe' . $row['stud_no'];
Now give the iframe that id:
<iframe ... id="<?= $iframeId ?>" ... ></iframe>
And make sure the button refers to that id:
<button ... onclick="document.title=''; document.getElementById('<?= $iframeId ?>').print();">...</button>
In the above code, I've created unique id's by prefixing them with studframe and then added the stud_no so it becomes id="studframe1", id="studframe2" and so on.
Then when referring to that specific iframe, we're fetching the iframe based on that unique id.

change <td> tag to editable and submit form after editing cakephp 2

I am working in a cakephp 2 project. On my view I have a table on which I want to make a cell editable. After editing I want want to save it.
I have tried contentEditable of HTML 5. By that I can edit but data is not passed to controller. I also tried innerHTML which change the cell to input but I can't write anything on the input field. How can I solve the problem? Thanks
<script>
function makeInput(e) {
document.getElementById('test').innerHTML= "<input type='text'>"
}
function formSubmit2(){
document.getElementById('StockReportEditForm').submit();
}
</script>
<?php if(!empty($results)): ?>
<?php echo $this->Form->create('StockReport',array('url'=>array('type'=>'post','action'=>'edit')));?>
<table>
<tr>
<th>Year Week</th>
<th>Counter</th>
<th>Refrigerator</th>
<th>Freezer</th>
<th>Summary</th>
</tr>
<?php foreach ($results as $result): ?>
<?php $today = date("Y-m-d", strtotime("+1 week"));
$date = new DateTime($today);
$week = $date->format("W");
$week_int = (int)$week;
$week_int=$week_int-1;
$last_week = (string)$week_int;
$year = date("Y"); ?>
<tr>
<?php if($year.$last_week==$result['StockReport']['yearweek']): ?>
<td onclick="makeInput(this)" id="test">
<?php echo $result['StockReport']['yearweek']; ?>
<?php else:?>
<td><?php echo $result['StockReport']['yearweek'];?></td>
<?php endif ?>
</td>
<td><?php echo $result['StockReport']['counter']; ?></td>
<td><?php echo $result['StockReport']['refrigerator']; ?></td>
<td><?php echo $result['StockReport']['freezer']; ?></td>
<td><?php echo $result['StockReport']['summary']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
// echo $this->Form->submit('Submit');
$options = array(
'label' => 'Submit',
'div' => array(
'class' => 'glass-pill',
),
'id'=>'submitButton',
'onclick'=>'formSubmit2()'
);
echo $this->Form->end($options); ?>
<?php endif; ?>
I would create a hidden input within the td element and when the form is submitted I copy the htmlinner of the contenteditabe to the hidden input value.

JavaScript function within php to show more info on-click (Database Query)

The below code is connected to a local database that holds customer information. First, the page is supposed to display the customer name and country, but should display more information for that particular customer once the name is clicked.
The problem with this code below is that it will only always display the first customers information, no matter which name is clicked. Can anyone see where I might be going wrong? Do I need to create an ID for each button (customer name)? Bare in mind that there are a lot of customers in the database and I feel that an ID for each would be very difficult to implement, especially if the database gets modified.
<DOCTYPE html!>
<html>
<head>
<link rel="stylesheet" type="text/css" href="a3.css">
</head>
<body>
<div class="nav">
<?php include 'nav.php';
?>
</div>
<?php include 'dbconfig.php';
$sql ='SELECT * FROM `customers` ORDER BY `customers`.`country` ASC';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
?>
<div class = "main">
<table>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><button onclick = "showCustomer();"/><?php echo ($r['customerName'])?></td>
<td><?php echo ($r['country'])?></td>
</tr>
<tr id ="extra" style="display:none">
<td><?php echo ($r['customerNumber'])?></td>
<td><?php echo ($r['contactLastName'])?></td>
<td ><?php echo ($r['contactFirstName'])?></td>
<td><?php echo ($r['phone'])?></td>
<td><?php echo ($r['addressLine1'])?></td>
<td><?php echo ($r['addressLine2'])?></td>
<td><?php echo ($r['city'])?></td>
<td><?php echo ($r['state'])?></td>
<td><?php echo ($r['postalCode'])?></td>
<td><?php echo ($r['salesRepEmployeeNumber'])?> </td>
<td><?php echo ($r['creditLimit'])?></td>
</tr>
<?php endwhile; ?>
</table>
</div>
<div class="foot">
<?php include 'foot.php'; ?>
</div>
<script>
function showCustomer() {
var showForm=document.getElementById('extra');
if (showForm.style.display="none") {
showForm.style.display ="";
alert('ok');
console.log(showForm);
}
else if (showForm.style.display=""){
showForm.style.display ="none";
}
}
</script>
</body>
</html>
</DOCTYPE>
All your <tr> have the same id="extra". As suggested, you need a different "id" for each of them, this is how : I created a variable $i (line 18), that is increased right after the while (line 20), inserted as parameter in the call for showCustomer (line 23), inserted at the end of the id "extra" (line 27), finally, on the bottom, where function showCustomer is declared, the parameter "index" is inserted at the end of the id "extra".
<DOCTYPE html!>
<html>
<head>
<link rel="stylesheet" type="text/css" href="a3.css">
</head>
<body>
<div class="nav">
<?php include 'nav.php';
?>
</div>
<?php include 'dbconfig.php';
$sql ='SELECT * FROM `customers` ORDER BY `customers`.`country` ASC';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
?>
<div class = "main">
<table>
<?php $i = 0;
while ($r = $q->fetch()):
$i++; // UNIQUE INDEX FOR EACH CUSTOMER.
?>
<tr>
<td><button onclick = "showCustomer('<?php echo $i;?>');"/>
<?php echo ($r['customerName'])?></td>
<td><?php echo ($r['country'])?></td>
</tr>
<tr id ="extra<?php echo $i;?>" style="display:none">
<td><?php echo ($r['customerNumber'])?></td>
<td><?php echo ($r['contactLastName'])?></td>
<td ><?php echo ($r['contactFirstName'])?></td>
<td><?php echo ($r['phone'])?></td>
<td><?php echo ($r['addressLine1'])?></td>
<td><?php echo ($r['addressLine2'])?></td>
<td><?php echo ($r['city'])?></td>
<td><?php echo ($r['state'])?></td>
<td><?php echo ($r['postalCode'])?></td>
<td><?php echo ($r['salesRepEmployeeNumber'])?> </td>
<td><?php echo ($r['creditLimit'])?></td>
</tr>
<?php endwhile; ?>
</table>
</div>
<div class="foot">
<?php include 'foot.php'; ?>
</div>
<script>
function showCustomer( index ) { // UNIQUE INDEX AS PARAMETER.
var showForm=document.getElementById('extra' + index); // USING INDEX.
if (showForm.style.display="none") {
showForm.style.display ="";
alert('ok');
console.log(showForm);
}
else if (showForm.style.display=""){
showForm.style.display ="none";
}
}
</script>
</body>
</html>
</DOCTYPE>

how to change user id to their name in php?

i need help. i have a table that i need to view the data of the user that already doing report. when the technician update the form, user can know who is the responsible person for their report. upon update the technician will send the id to the database but the problem is the user will see the id when they check the report. i need to change the id to the technician name.
<tr bgcolor="#00aeef">
<td>Tarikh</td>
<td>Staff ID</td>
<td>Kerosakan</td>
<td>Nyatakan</td>
<td>Maklumat Kerosakan</td>
<td>Lokasi Kerosakan</td>
<td>Nama Pegawai</td>
<td>Status</td>
<td>Catatan</td>
</tr>
<?php include('../dbcon.php');
$username=$_SESSION['username'];
$username_view=$_SESSION['username_view'];
$qry=mysql_query("SELECT * FROM aduan_form where staff_id='$username' ORDER BY tarikh DESC") or die(mysql_error());
?>
<?php while ($row_view = mysql_fetch_array($qry)){ ?>
<?php if ($totalRows_view > 0) { // Show if recordset not empty ?>
<?php } // Show if recordset not empty ?>
<tr>
<td><?php echo $row_view['tarikh']; ?></td>
<td><?php echo $row_view['staff_id']; ?></td>
<td><?php echo $row_view['kerosakan']; ?></td>
<td><?php echo $row_view['nyatakan']; ?></td>
<td><?php echo $row_view['m_kerosakan']; ?></td>
<td><?php echo $row_view['tempat_rosak']; ?></td>
<td><?php echo $row_view['nama_pegawai']; ?></td>
<td><?php echo $row_view['status']; ?></td>
<td><?php echo $row_view['catatan']; ?></td>
<?php } while ($row_view = mysql_fetch_assoc($view)); ?></tr>
</table>
here is my current code.
i do try to put some code that will allow the id to change to the name but it view the same name and it is even in the table that is still not been update.
<?php include('../dbcon.php');
$username=$_SESSION['username'];
$username_view=$_SESSION['username_view'];
$qry1=mysql_query("SELECT * FROM login,aduan_form where aduan_form.nama_pegawai=login.username ORDER BY aduan_form.tarikh DESC") or die(mysql_error());
?>
<?php while ($row_view1 = mysql_fetch_array($qry1)){ ?>
how should i do to make it right? i need the table to view the right technician name based on the right id and also display the name only in the table that already been update.
Hope I got your database schema right. So you can query like:
$qry1=mysql_query("
SELECT aduan_form.*, login.username
FROM aduan_form
LEFT JOIN login
ON login.id = aduan_form.staff_id
WHERE aduan_form.nama_pegawai=login.username
ORDER BY aduan_form.tarikh DESC")
or die(mysql_error());
and change your html output to something like:
...
<td><?php echo $row_view['tarikh']; ?></td>
<td><?php echo $row_view['username']; ?></td>
<td><?php echo $row_view['kerosakan']; ?></td>
...

Categories