I am trying to add a delete button to my DataTables table, but I don't know how I can post the username of the selected row when Delete button is pressed.
Here is how the DataTables is printed:
<div class="panel-body">
<div class="dataTable_wrapper">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>Fullname</th>
<th>Class ID</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
while($listDetailsRow = mysql_fetch_array($listDetails)){
?>
<tr>
<td class="success"><?=$listDetailsRow['username']?></td>
<td class="info"><?=$listDetailsRow['email']?></td>
<td class="success"><?=$listDetailsRow['fullname']?></td>
<td class="info"><?=$listDetailsRow['class_id']?></td>
<td>
<form id='delete' action='deleteUser.php' method='post'>
<button type="submit" name="submit" class="btn btn-danger" href="deleteUser.php">
Delete
</button>
</form>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
So it is printed by php until all has been printedm then it stops...
The deleteUsers.php file is as follows:
include('../details.php');
$userDel = $_POST($listDetailsRow['username']);
echo $userDel;
// mysql_query("DELETE * FROM users WHERE username='$userDel'") or die(mysql_error());
I am just trying to get it to print the username from the row of which the delete button was pressed.
Is this possible? Not very good with JS or Ajax so all help is appreciated.
<form id='delete' action='deleteUser.php' method='post'>
<input type='hidden' name='username' value="<?=$listDetailsRow['username'];?>" > <--add this line
<button type="submit" name="submit" class="btn btn-danger" href="deleteUser.php">
Delete
</button>
</form>
php:
$userDel = $_POST['username'];
You can use an a element instead of a form within a button element. See this code:
<tbody>
<?php
while($listDetailsRow = mysql_fetch_array($listDetails)){
?>
<tr>
<td class="success"><?=$listDetailsRow['username']?></td>
<td class="info"><?=$listDetailsRow['email']?></td>
<td class="success"><?=$listDetailsRow['fullname']?></td>
<td class="info"><?=$listDetailsRow['class_id']?></td>
<td>
<!------------- New -------------->
<?php
$params = array( 'username' => $listDetailsRow['username'] );
$href = "deleteUser.php?" . http_build_query($params);
?>
<a class="btn btn-danger" href="<?php echo $href; ?>" > Delete </a>
<!------------- End New -------------->
</td>
</tr>
<?php
}
?>
</tbody>
In deleteUser.php do this:
$userDel = isset($_GET['username']) ? $_GET['username'] : null;
Related
I have a table of each request filed by the user with corresponding actions to it. When I click on the update button, it doesnt load the view after submitting it.
This is my html code:
<table id="dataTableRequestDrafts" class="table table-striped table-bordered data-table-drafts" style="width:100%">
<thead>
<tr>
<th style="display: none;">Request ID</th>
<th>Request Type</th>
<th>Date Requested</th>
<th>Date Last Updated</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($param_drafts as $drafts){?>
<tr>
<td class="id" style="display: none;"><?php echo $drafts['idx']?></td>
<td class="type"><?php echo $drafts['request_type']?></td>
<td><?php echo $drafts['date_requested']?></td>
<td><?php echo $drafts['date_updated']?></td>
<td class="text-warning"><?php echo $drafts['status']?></td>
<td align="center">
<form action="<?php echo base_url('dashboard/staff/request/update_request_view'); ?>" method="post">
<input type="text" name="request_id" value="<?php echo $drafts['idx']?>" style="display: none;">
<button type="button" class="btn waves-effect waves-light btn-primary view-button"><i class="fa fa-eye"></i> View</button> <button type="submit" class="btn waves-effect waves-light btn-info update-button"><i class="fa fa-edit"></i> Update</button> <button type="button" class="btn waves-effect waves-light btn-danger delete-button" data-url="<?php echo base_url('dashboard/staff/request/delete');?>"><i class="fa fa-trash-o"></i> Delete</button>
</form>
</td>
</td>
</tr>
<?php } ?>
</tbody>
</table>
This is my function in the controller:
public function update_request_view()
{
$data = new stdClass;
$data->param_menu = 'request';
$idx = $this->input->post('request_id');
$type = $this->staffrequisition_model->getRequestType($idx);
$typeLower = strtolower($string = str_replace(' ', '', $type));
$commonContents = $this->staffrequisition_model->selectItem(array('idx'=> $idx));
$uncommonContents = json_decode($commonContents->contents);
$data->param_request = array_merge((array) $commonContents, (array) $uncommonContents);
$this->load->view('dashboard/staff/update_'.$typeLower.'_view', $data);
}
The view is seen in the response but it doesnt load the view. I was gonna use ajax but it would be tedious to load the data of the request in each input in the view and I want to return the whole html page not just a div.
Can you show the response here.
sometimes the response can't handle by browser. so it can't appear
First I will tell what I want to achieve, and then I will explain how I was trying to do it.
I have two tables, one stores type of vessels with a description and their own Id. Then I have another table in wich the vessel type has been stored as text. My goal is to select each one (both records in both tables) with a checkbox in both and store in the table 2 the Id from the table 1.
I will introduce data, to help.
Table 1
1|Balandra|Some description
2|Bergantin|Some description
3|Whatever |Whatever.....
Table2
Balandra
Bergantin
Whatever
Then, I have created a php page that shows both tables with the checkbox I mentioned above. Checkboxes store the Table1 Id and Table2 vesseltypename.
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th class="hidden-xs">idtiponavio</th>
<th>Tipo de Navío</th>
<th>Descripción</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosdyncoop as $key => $navio) { ?>
<tr>
<td align="center">
<a href=<?php echo '../vista/modificar.php?id=' .
$navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-
pencil"></em></a>
<a href=<?php echo '../datos/borrar.php?id=' .
$navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-
trash"></em></a>
</td>
<td class="hidden-xs"><?php echo
$navio['idtiponavio']; ?></td>
<td><?php echo $navio['tiponavio']; ?></td>
<td><?php echo $navio['descripcion']; ?></td>
<td><input type="checkbox" name="agruparid" value=<?
php echo $navio['idtiponavio']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel-body paneltodo">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th>Tipo de Navío</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosforsea as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-arrow-circle-o-left"></em>
</td>
<td><?php echo $navio['typevessel']; ?></td>
<td><input type="checkbox" name="agruparvessel" value=<?php echo $navio['typevessel']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
So, I want to check both table records and store the table1 Id in the table2 idtypevessel field.
I thought that a php file could store both items and call the update function with those parameters, like this:
<?php
require './modelo.php';
$idnavio = $_GET['agruparid'];
$vessel = $_GET['agruparvessel'];
Any suggestions, because I think I have to do a button to submit this parameters, but it must be working on both tables, and I don't know how to access both foreach loop at the same time.
Thanks in advance.
E. Salas
review bellow reference code to submit multi selected checkbox values
for multi selected checkbox submission you must use [] operator after name attribute in html
index.php
<form action="/checkbox.php" method="post">
<strong>Cars:</strong><br>
<?php
$cars = array("Volvo", "BMW", "Toyota");
$colors = array("Red", "Green", "Black");
foreach($cars as $single){
?>
<input type="checkbox" name="cars[]" value="<?php echo $single; ?>">
<?php
}
<br>
<strong>colors:</strong><br>
foreach($colors as $single){
?>
<input type="checkbox" name="colors[]" value="<?php echo $single; ?>">
<?php
}
?>
<br>
<input type="submit" value="Submit!">
</form>
checkbox.php
<?php
echo "<pre>";
var_dump($_POST);
exit;
In your case:
<form action="/checkbox.php" method="post">
<div class="col-md-6">
<div class="panel-body">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th class="hidden-xs">idtiponavio</th>
<th>Tipo de Navío</th>
<th>Descripción</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosdyncoop as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-pencil"></em>
<em class="fa fa-trash"></em>
</td>
<td class="hidden-xs"><?php echo $navio['idtiponavio']; ?></td>
<td><?php echo $navio['tiponavio']; ?></td>
<td><?php echo $navio['descripcion']; ?></td>
<td><input type="checkbox" name="agruparid[]" value=<?php echo $navio['idtiponavio']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel-body">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th>Tipo de Navío</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosforsea as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-arrow-circle-o-left"></em>
</td>
<td><?php echo $navio['typevessel']; ?></td>
<td><input type="checkbox" name="agruparvessel[]" value=<?php echo $navio['typevessel']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<input type="submit" value="Submit!">
</form>
Finally I solved this with Javascript. One of my partners helped me, I post this to help people in my situation.
I created a script, here is the code:
<script type="text/javascript">
function cogeNavioDyncoopnet(){
var checkedValueNavioD = null;
var inputElements = document.getElementsByClassName('checknaviod');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValueNavioD = inputElements[i].value;
break;
}
}//return checkedValueNavioD;
var input_nav_dyn = document.getElementById("nav_dyn");
input_nav_dyn.value = checkedValueNavioD;
}
function cogeNavioForSea(){
var checkedValueNavioFs = null;
var inputElements = document.getElementsByClassName('checknaviofs');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValueNavioFs = inputElements[i].value;
break;
}
}//return checkedValueNavioFs;
var input_nav_fs = document.getElementById("nav_fs");
input_nav_fs.value = checkedValueNavioFs;
}
</script>
Then I created a Form below, that collects values and sends them to my .php control file.
<div class="hidden-xs">
<form class="hidden-xs" method="POST"
action="../datos/actualizarnavios.php">
<input id="nav_dyn" type="text" name="idnaviodyncoop">
<input id="nav_fs" type="text" name="navioforsea" >
<input id="botonasignar" type="submit" name="enviardatosdynfs">
</form>
</div>
I hope this helps. Thanks for the feedback, as always.
There's a table that is listed out from SQL and I have added a button on every row which I want it to link to another page.
The problem is that I would like that button to open up the correct row of data.
<div class="container">
<div class="jumpbotron">
<table class="table table-striped">
<thead>
<tr class="success">
<td>Date</td>
<td>Subject</td>
<td>Details</td>
<td>Status</td>
<td>ticket id</td>
<td>Actions</td>
</tr>
</thead>
<?php
while($row=mysqli_fetch_array($result))//while look to fetch the result and store in a array $row.
{
$date=$row[2];
$subject=$row[3];
$detail=$row[4];
$status=$row[5];
$tickid=$row[0];
?>
<form method="post">
<tbody method="post">
<td class="col-md-1"><?php print_r($date); ?></td>
<td class="col-md-1"><?php print_r($subject); ?></td>
<td class="col-sm-2"><?php print_r($detail); ?> </td>
<td class="col-md-1"><?php print_r($status); ?></td>
<td class="col-md-1"><?php echo $tickid ; ?></td>
<td class="col-md-1"><a href="detail.php" name="submit" id="submit"class="btn btn-sm btn-success">Details</td>
</tbody>
</form>
<?php } ?>
</table>
</div>
</div>
You can get the $tickid on your detail.php using $_GET['id'], in addition the user can copy the URI and share your current row's details.
<a href="detail.php?id=<?php echo $tickid; ?>" name="submit" id="submit" class="btn btn-sm btn-success">
I have got an ebooks table in the database where path of pdf is defined.What I want is, when a user clicks on the download button, pdf get download.But the download button is not working and I'm also not getting any error from Php.Please help! Thank you.
Code:
<div id="data">
<?php
if (isset($_POST['submit'])) {
$title = $_POST['title'];
$name = $_POST['name'];
$count = 1;
$query = "SELECT * FROM ebooks WHERE $title LIKE '%".$name."%'";
$display = $obj->run_query($query);
?>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr align="center">
<td>No.</td>
<td>Title</td>
<td>Author</td>
<td>Publication</td>
<td>Status</td>
</tr>
</thead>
<tbody>
<?php
foreach ($display as $key) { ?>
<tr align="center">
<td><?php echo $count++;?></td>
<td><?php echo $key['ebook_title'];?></td>
<td><?php echo $key['ebook_author'];?></td>
<td><?php echo $key['ebook_publication'];?></td>
<td><a style="color:white;" href="/admin/ebooks/<?php echo $key['ebook_url'];?>" class="btn btn-round btn-primary">Download</a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php } ?>
</div>
</div>
You have to add download before ending of anchor tag try this :
<a style="color:white;" href="/admin/ebooks/<?php echo $key['ebook_url'];?>" class="btn btn-round btn-primary" download>Download</a>
I am having trouble when bootstrap table is installed. When I use data-toggle="table" to activate bootstrap I am not able to post checked value it always display NULL that means it is not checked . If I create normal table everything works great.
Does anyone know how to solve this issue, is this a bug?
<form method="post" action="unos.php">
<button type="submit" class="btn btn-primary" name="izmjena">Izmjena</button>
<table data-toggle="table" data-show-columns="true" data-click-to-select="true"
data-search="true" data-show-refresh="true" data-show-export="true" >
<tr>
<th data-field="artikli" data-checkbox="true" name="artikli"> </th>
<th data-field="sifra" data-sortable="true">Sifra</th>
<th data-field="name" data-sortable="true">Naziv</th>
</tr>
$q = $conn->query($sql);
while ($r = $q->fetch()): ?>
<tr>
<td><input type="checkbox" name="artikli" value="<?php echo $r['ArtId'];?>"></td>
<td><?=$r['ArtSifra'] ?></td>
<td><?=$r['ArtNaziv'] ?></td>
</tr>
<?php endwhile; ?>
</table>
</form>
// unos.php file
if (isset($_POST["izmjena"])) {
$id=(!empty($_POST["artikli"])) ? $_POST["artikli"] : '';
var_dump($id);
echo $id;
}