Ajax table doesn't show anything - php

I'm creating a table using ajax and php but there's one problem, the table isn't showing in my div. I'm really new to ajax so I don't really fully understand it yet.
Here's my div:
<div class="body" id="live-data">
</div>
Here's the ajax code:
$(document).ready( function() {
function fetch_data() {
$.ajax({
url:"fetch.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
fetch_data();
});
And here's fetch.php:
<?php
include('../global/db.php');
$output = '';
$sql ="SELECT * FROM students WHERE status = '0' AND stud_grade = '$level_id' ORDER BY date_enrolled DESC";
$result = mysqli_query($db, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover dataTable js-exportable">
<thead>
<tr>
<th width="135" class="noExport">Action</th>
<th width="90">LRN</th>
<th width="20">Level</th>
<th>Name</th>
<th width="20">Gender</th>
<th width="60">Type</th>
<th width="105" style="font-size: 14px!important;">Date Enrolled</th>
</tr>
</thead>
<tbody>';
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
$output .= '
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
';
}
}
else {
$output .= '
<tr>
<td colspan="12">Data not Found</td>
</tr>';
}
$output .= '
</tbody>
</table>
</div>';
echo $output;
?>
It would great if anyone could help because I just don't know why it doesn't work
Edit:
I've changed the code so it returns the data in the console tab and here's what shows up:
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover dataTable js-exportable">
<thead>
<tr>
<th width="135" class="noExport">Action</th>
<th width="90">LRN</th>
<th width="20">Level</th>
<th>Name</th>
<th width="20">Gender</th>
<th width="60">Type</th>
<th width="105" style="font-size: 14px!important;">Date Enrolled</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
</tbody>
</table>
</div>
So it clearly returns the correct data but it just doesn't show up in the live-data div

Maybe take a look in your html where is your div. Your div id says "live-data" and in the ajax code you mentioned to fetch data for div id "#live_data" instead of "#live-data".
Maybe changing them for same name can solve your problem. I would use for thr div id and in the ajax same id names like "#liveData".
Example (same code, just edited to the right IDs, compare with your original):
Your HTML div
<div class="body" id="liveData">
</div>
Your ajax code
$(document).ready( function() {
function fetch_data() {
$.ajax({
url:"fetch.php",
method:"POST",
success:function(data){
$('#liveData').html(data);
}
});
}
fetch_data();
});

Just append the correct Id of the div tag in sucess function

Related

How to show image in table using PHP

I tried the following code to show my SQL database data in a table in my dashboard
<tbody>
<?php
while ($row = $result->fetch_assoc()):?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><img src="<?php echo $row['image']; ?>" width=100px height=100px></td>;
<td><?php echo $row['class']; ?></td>
<td><?php echo $row['reg_no']; ?></td>
<td><?php echo $row['capacity']; ?></td>
<td><?php echo $row['oname']; ?></td>
<td><?php echo $row['province']; ?></td>
<td><?php echo $row['district']; ?></td>
<td><?php echo $row['contact']; ?></td>
<td><a href="#" class="btn btn-warning btn-circle editbtn">
<i class="fas fa-edit"></i></a></td>
<td><a href="#" class="btn btn-info btn-circle viewbtn">
<i class="fas fa-eye"></i></a></td>
<td><a href="#" class="btn btn-danger btn-circle deletebtn">
<i class="fas fa-trash"></i></a></td>
</tr>
<?php endwhile; ?>
</tbody>
But that image column image does not show has an image
Result show like this:
my result
How to solve that problem
To me it seems your are inserting to the src attribute the context of the file rather then the URL of the image.
you are saving the image content it self in the DB,
check this out :
https://www.codexworld.com/upload-store-image-file-in-database-using-php-mysql/

Button Does Not Update PHP SQL Server Table

I have this code and I want to make the BIT column Color 1 by pressing the check button and I really can't handle the php. I need help, please!
There is the table and how I access each data from the SQL server, it shows fine but the button is not doing anything when I'm trying to _POST in php. I have been looking for an answer but I couldn't find anything.
<table class="table table-hover">
<thead class="thead-light">
<tr>
<th scope="col">Nr Expediente</th>
<th scope="col">Fecha</th>
<th scope="col">Tipo</th>
<th scope="col">Responsable</th>
<th scope="col">Proveedor</th>
<th scope="col">Observaciones</th>
<th scope="col">Hora</th>
<th scope="col">Check</th>
</tr>
</thead>
<tbody>
<?php
$query="SELECT dbo.Transportes.IdExpediente, dbo.Operaciones.IdTransporte, dbo.Operaciones.Id, dbo.Operaciones.Color, dbo.Operaciones.CreadoPor, dbo.Expedientes.Numero, dbo.Expedientes.ProveedorNombre, dbo.Operaciones.Fecha, dbo.Operaciones.Tipo, dbo.Operaciones.Hora, dbo.Operaciones.Instrucciones FROM dbo.Expedientes
INNER JOIN dbo.Transportes ON dbo.Expedientes.Id = dbo.Transportes.IdExpediente INNER JOIN dbo.Operaciones ON dbo.Operaciones.IdTransporte = dbo.Transportes.Id
WHERE convert(date, convert(varchar(30), dbo.Operaciones.Fecha), 101) = CONVERT (date, GETDATE()) AND dbo.Operaciones.Tipo = 0 ORDER BY dbo.Operaciones.Id DESC";
$res=sqlsrv_query($con,$query);
while ($row=sqlsrv_fetch_array($res)){
?>
<tr <?php if ($row['Color'] == 1) {
echo 'class="table-success"';
} else { echo 'class="table-warning"';} ?> ><td><?php echo $row['Numero']; ?></td>
<td><?php echo $row['Fecha']->format('d/m/Y'); ?></td>
<td><?php if ($row['Tipo'] == 1) {
echo "Descarga";
} else { echo "Carga";} ?></td>
<td><?php echo $row['CreadoPor']; ?></td>
<td><?php echo $row['ProveedorNombre']; ?></td>
<td><?php echo $row['Instrucciones']; ?></td>
<td><?php echo $row['Hora']; ?></td>
<td>
<form method="post">
<div class="btn-group">
<button type="button" name="Checking" class="btn btn-success">Check</button>
<input type="hidden" name="id" value="' .$row['Id']. '"/>
<input type="hidden" name="color" value="1"/>
<button type="button" class="btn btn-success dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="sr-only">Toggle Dropdown</span>
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">In progress</a>
<a class="dropdown-item" href="#">Save for later</a>
<div class="dropdown-divider"></div>
<button type="butoon" name="Unchecking">Uncheck</a>
</div>
</div> </form></td></tr>
<?php
}
?>
</tr>
</tbody>
</table>
<?php if(isset($_POST['Checking'])){
$sql = "UPDATE dbo.Operaciones SET
Color = '1'
WHERE Id = {_POST['id']} ";
$result = sqlsrv_query($con,$sql) or die(sqlsrv_errors());
}
?>
change type="button" to type="submit"
and
change
$sql = "UPDATE dbo.Operaciones SET
Color = '1'
WHERE Id = ".$_POST['id'] ;

I want to hide table buttons

I have one table and in this table, I have two button Update and Disable I want to hide those buttons
I trying like this
<table id="example1" class="table table-bordered table-striped table-sm" style=" overflow: auto; ">
<tr>
<th>Dispatch Challan No</th>
<th>Date</th>
<th>From</th>
<?php
$hide = 'OFF';
if($hide == 'ON') { ?>
<th>Update</th>
<th>Delete</th>
<?php } ?>
</tr>
<?php foreach($dispatch as $dis){?>
<tr>
<td><?php echo $dis->disp_ch_no;?></td>
<td><?php echo date("d-m-Y", strtotime($dis->disp_ch_date));?></td>
<td><?php echo $dis->from_branch_name;?></td>
<td><a class="btn btn-success btn-sm" href="<?php echo base_url(); ?>booking/dispatch_challan/DispatchChallanController/updateDispatchChallanPage?disp_id=<?php echo $dis->disp_id; ?>"><i class="fa fa-pencil" > Update</i></a></td>
<td><a class="btn btn-danger btn-sm" onclick="delete_dispatch('<?php echo $dis->disp_id; ?>');" title="Click here to delete your Dispatch record"><i class="fa fa-trash" style="color: #fff;"> Disable </i> </a>
</td>
</tr>
<?php }?>
</table>
this is my table
How can I hide these buttons
Try this,
<?php
$thswitch = 'OFF';
if($thswitch == 'ON') { ?>
<th>Update</th>
<th>Delete</th>
<?php } ?>
whenever you want to show just $thswitch = 'ON'; it
You have to check the if () for each row as we did in th. Like:
<table id="example1" class="table table-bordered table-striped table-sm" style="overflow: auto;">
<tr>
<th>Dispatch Challan No</th>
<th>Date</th>
<th>From</th>
<?php $hide = 'OFF';
if ($hide == 'ON') { ?>
<th>Update</th>
<th>Delete</th>
<?php } ?>
</tr>
<?php foreach($dispatch as $dis) { ?>
<tr>
<td> <?php echo $dis->disp_ch_no;?> </td>
<td> <?php echo date("d-m-Y", strtotime($dis->disp_ch_date));?> </td>
<td> <?php echo $dis->from_branch_name;?> </td>
<?php if ($hide == 'ON') { ?>
<td>
<a class="btn btn-success btn-sm" href="<?php echo base_url(); ?>booking/dispatch_challan/DispatchChallanController/updateDispatchChallanPage?disp_id=<?php echo $dis->disp_id; ?>"><i class="fa fa-pencil" > Update</i></a>
</td>
<td>
<a class="btn btn-danger btn-sm" onclick="delete_dispatch('<?php echo $dis->disp_id; ?>');" title="Click here to delete your Dispatch record"><i class="fa fa-trash" style="color: #fff;"> Disable </i> </a>
</td>
<?php } ?>
</tr>
<?php } ?>
</table>
Or by using CSS you can hide this columns as:
Create a CSS class like:
.hidethisColumn { display: none !important; }
Use it in you table as:
<th class="<?=(($hide == 'ON')? 'hidethisColumn' : '')?>">Update</th>
<th class="<?=(($hide == 'ON')? 'hidethisColumn' : '')?>">Delete</th>
Similarly in rows inside foreach():
<td class="<?=(($hide == 'ON')? 'hidethisColumn' : '')?>">
<a class="btn btn-success btn-sm" href="<?php echo base_url(); ?>booking/dispatch_challan/DispatchChallanController/updateDispatchChallanPage?disp_id=<?php echo $dis->disp_id; ?>"><i class="fa fa-pencil" > Update</i></a>
</td>

Add input form inside a table

So inside my table before tbody tag, I inserted a form inside tbody. However while submitting its form, the url is getting btnchange.php?qty=1 instead of btnchange.php?prod_id=cartno.
shopcart.php
<?php // start
$stmt = $DB_con->prepare("SELECT * FROM cart WHERE cartuser = '$userid' and cartpend='IN CART'");
$stmt->execute();
if($stmt->rowCount() > 0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row); //end ?>
<form method="get" action="btnchange.php?prod_id=<?php echo $row['cartno'] ?>">
<tbody>
<tr>
<td><?php echo $row['cartno']; ?></td>
<td><?php echo $row['cartname']; ?></td>
<td><?php echo $row['cartprice']; ?></td>
<td><input type="text" name="qty" value="<?php echo $row['cartqty']; ?>"></td>
<td><?php echo $row['cartsub']; ?></td>
<td><span class="label label-primary"><?php echo $row['cartpend']; ?></span></td>
<td><button type="submit" name="submit">Submit Quantity</button></b>
<td><a class="btn btn-danger btn-xs" href="?delete_id=<?php echo $row['cartno']; ?>" title="click for delete" onclick="return confirm('Sure to Delete?')"><i class="fa fa-trash-o"></i> Remove</a></td>
</tr>
</tbody>
</form>
This is my btnchange.php code, this is what i used to get the prod_id code and the qty input from shopcart.php.
btnchange.php
//get prod id
if(isset($_GET['prod_id']) && !empty($_GET['prod_id']))
{
$prod_code = $_GET['prod_id'];
$newqty = $_GET['qty'];
You can't put query parameters in the action attribute. You need to use a hidden input instead:
<?php // start
$stmt = $DB_con->prepare("SELECT * FROM cart WHERE cartuser = '$userid' and cartpend='IN CART'");
$stmt->execute();
if($stmt->rowCount() > 0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row); //end ?>
<form method="get" action="btnchange.php">
<input type="hidden" name="prod_id" value="<?php echo $row['cartno']; ?>">
<tbody>
<tr>
<td><?php echo $row['cartno']; ?></td>
<td><?php echo $row['cartname']; ?></td>
<td><?php echo $row['cartprice']; ?></td>
<td><input type="text" name="qty" value="<?php echo $row['cartqty']; ?>"></td>
<td><?php echo $row['cartsub']; ?></td>
<td><span class="label label-primary"><?php echo $row['cartpend']; ?></span></td>
<td><button type="submit" name="submit">Submit Quantity</button></b>
<td><a class="btn btn-danger btn-xs" href="?delete_id=<?php echo $row['cartno']; ?>" title="click for delete" onclick="return confirm('Sure to Delete?')"><i class="fa fa-trash-o"></i> Remove</a></td>
</tr>
</tbody>
</form>

Open in new window

Trying to open a new window using a tag using target attribute but it is not working. Here is my code:
<tr>
<td>
<?php echo $count;?>
</td>
<td><a class="oiu" href=<?php echo "eventdetail.php?idno=".$row[ 'E_id'];?>><u><?php echo $row['E_name'];?></u></a>
</td>
<td><a class="oiu" href=<?php echo "http://maps.google.com/maps?saddr=".$cadd. "&daddr=".$vadd;?> target="_blank" ><u><?php echo $row['Venue'];?><u></a>
</td>
<td>
<?php echo $row[ 'EDate']. ' '.$time;?>
</td>
<!-- <td><div id="remain"><?php echo "$days, $hours:$minutes:$seconds";?></div></td> -->
<td> <a class="icon1" href=<?php echo "update.php?idno=".$eid;?>><span class="glyphicon glyphicon-pencil pencil"></span></a><a class="icon2" href="#myModal2" data-toggle="modal"><span class="glyphicon glyphicon-trash trash"></span></a>
</td>
</tr>
<?php $count++;
Make your target="_blank" first then give your href="link" as given below and close the underline tag </u>.
Try the following code:
<tr>
<td><?php echo $count;?></td>
<td ><a class="oiu" href=<?php echo "eventdetail.php?idno=".$row['E_id'];?>><u><?php echo $row['E_name'];?></u></a></td>
<td><a class="oiu" target="_blank" href=<?php echo "http://maps.google.com/maps?saddr=".$cadd."&daddr=".$vadd;?> ><u><?php echo $row['Venue'];?></u></a></td>
<td><?php echo $row['EDate'].' '.$time;?></td>
<!-- <td><div id="remain"><?php echo "$days, $hours:$minutes:$seconds";?></div></td> -->
<td> <a class="icon1" href=<?php echo "update.php?idno=".$eid;?>><span class="glyphicon glyphicon-pencil pencil"></span></a><a class="icon2" href="#myModal2" data-toggle="modal"><span class="glyphicon glyphicon-trash trash"></span></a></td>
</tr>

Categories