Passing one of the <td> value to controller when clicking a button - php

I have a table like this:
When I click the Delete! button, I want to pass the role ID to the controller.
After looking for a lot of cases in stackoverflow. I've just succeeded to retrieve the role ID using JS.
Code like this:
$(".deleteButton").click(function() {
var $row = $(this).closest("tr");
var $text = $row.find("td:first-child").text();
// Let's test it out
alert($text);
});
But the problem is, I don't how to pass it back to the button, so then the button can pass it to the controller.
Is there any simple way to pass it to the controller? Maybe without retrieving the role ID first, and when click the button the value passed and the controller called at once?
This is my code:
<table class="table" id="myTable">
<thead class="text-primary">
<th class="col-md-1">ID Role</th>
<th class="col-md-3">Role</th>
<th class="col-md-3">Jumlah Member</th>
<th></th>
</thead>
<tbody>
<?php
foreach($result as $row) { ?>
<tr>
<td><?php echo $row->id_role ?></td>
<td><?php echo $row->role ?></td>
<td><?php echo $row->jumlah ?></td>
<td>
<button type="button" class="btn btn-success btn-sm editButton">Edit!</button>
<button type="button" class="btn btn-danger btn-sm deleteButton">Delete!</button>
</td>
</tr>
<?php
} ?>
</tbody>
</table>

Do you have to use JavaScript? If not, this is a simple task for a form
<form action="path/to/your/controller" method="post">
<table class="table" id="myTable">
<!-- thead, etc -->
<tbody>
foreach($result as $row) : ?>
<tr>
<td><?= $row->id_role ?></td>
<td><?= $row->role ?></td>
<td><?= $row->jumlah ?></td>
<td>
<button type="submit" name="edit" value="<?= htmlspecialchars($role->id_role) ?>"
class="btn btn-success btn-sm editButton">Edit!</button>
<button type="submit" name="delete" value="<?= htmlspecialchars($role->id_role) ?>"
class="btn btn-danger btn-sm deleteButton">Delete!</button>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</form>
Your controller would then receive either $_POST['edit'] or $_POST['delete'] with the role ID as the value.
If you're interested in securing your form from possible cross-site-request-forgery attacks, CodeIgniter has a built-in solution.
Otherwise, I would just add the id_role property to the button, eg
<button type="button" class="btn btn-danger btn-sm deleteButton" value="<?= htmlspecialchars($role->id_role) ?>">Delete!</button>
and in your JS
$('.deleteButton').on('click', function(e) {
var roleId = this.value
$.ajax({
url: '/roles/' + roleId, // assuming a REST API
method: 'DELETE'
}).done(function() {
alert('Deleted role ' + roleId);
})
})

You assign the role id to another <td> which is a little bit longer to get the id by jQuery. You easily get the click button id just give to an id like.
<button type="button" id="<?php echo $row->id_role ?>" class="btn btn-danger btn-sm deleteButton">Delete!</button>
and then you can easily get the specific row id
$(".deleteButton").click(function() {
var $text= $(this).attr("id");
alert($text);
});
and then you can send that id to controller by ajax.

Related

How to update database from Menu?

<div class="container">
<h3 class="text-center">Hotel/Admin Joint Panel</h3>
<table class="table table-bordered">
<tr>
<th>Food_ID</th>
<th>Cuisines</th>
<th >Description</th>
<th >Price</th>
<th >Image</th>
<th >Date</th>
<th width="50px">Action</th>
</tr>
<?php
include_once 'mainclass.php';
$confirmThread = new menuDatabase();
$sql = "SELECT * FROM gallery";
$users = mysqli_query($confirmThread->getCon(),$sql);
while($user = $users->fetch_assoc()){
?>
<tr id="<?php echo $user['idGallery'] ?>" >
<td ><?php echo $user['idGallery'] ?></td>
<td><?php echo $user['titleGallery'] ?></td>
<td><?php echo $user['descGallery'] ?></td>
<td><?php echo $user['price'] ?></td>
<!-- <td><?php echo ("data:image/jpeg;base64,".base64_encode($user['image'] )) ?></td> -->
<td><?php echo '<img src="data:image/jpeg;base64,'.base64_encode($user['image'] ).'">'
?></td>
<td width="100px"><?php echo $user['samaye'] ?></td>
<td>
<button class="btn btn-danger btn-sm remove">Delete</button>
<button style="margin-top: 2px" id="createOnly" class="btn btn-danger"> Create Menu</button>
<button style="margin-top: 2px" id="updateOnly" id="hi" class="btn btn-danger btn-sm "> <a class="update" href="upload.php">Update</a></button>
</td>
</tr>
<?php } ?>
</table>
</div> <!-- container / end -->
I wanted to update the database using an update button which is in Menu.php. What I wanted is, when I clicked on the update button(in menu.php) it should grab the food_id(primary key) and remember this for Upload.php, from where I am doing real updating by getting all new values.
You can change the following statement to
<td>
<button class = "btn btn-danger btn-sm remove">Delete</button>
<button style = "margin-top: 2px" id = "createOnly" class = "btn btn-danger"> Create Menu</button>
<button style = "margin-top: 2px" id = "updateOnly" id = "hi" class = "btn btn-danger btn-sm"> <a class = "update" href = "upload.php">Update</a></button>
</td>
this. This will act as a form (Only for the update button) and you can get these values to your update.php. The id that you need is hidden. From update.php you can do what you need to.
<td>
<button class = "btn btn-danger btn-sm remove">Delete</button>
<button style = "margin-top: 2px" id = "createOnly" class = "btn btn-danger"> Create Menu</button>
<form method = "post" action = "upload.php">
<input type = "hidden" name="food_id" value="<?php echo $user['idGallery']; ?>"/>
<button type = "submit" style = "margin-top: 2px" id = "updateOnly" id = "hi" class = "btn btn-danger btn-sm">Update</button>
</form>
</td>
Or else you can do this by using Query Parameter like this,
<td>
<button class = "btn btn-danger btn-sm remove">Delete</button>
<button style = "margin-top: 2px" id = "createOnly" class = "btn btn-danger"> Create Menu</button>
<button style = "margin-top: 2px" id = "updateOnly" id = "hi" class = "btn btn-danger btn-sm"> <a class = "update" href = "upload.php?foodID=<?php echo $user['idGallery']; ?>">Update</a></button>
</td>
and from your update.php you can get the result like this,
$foodID = $_GET["foodID"];
Hope this helps you!

How to get id from php loop in jquery

Hi I'm trying to get the id for each row shown in while loop in my jquery function
Here are the request and view (PHP code)
<div class="container">
<table class="table table-striped">
<div class="table responsive">
<thead class="black white-text">
<tr>
<th>#</th>
<th>Nom et Prénoms</th>
<th>Fonction</th>
<th>Université</th>
<th>Email</th>
<th>Contact</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$result = $connection->query($stmt) ;
if($result->rowCount() > 0)
{
while($row = $result->fetch(PDO::FETCH_BOTH))
{
echo '<tr>
<td scope="row">'.$row["id"].'</td>
<td>'.$row["nom"].'</td>
<td> '.$row["fonction"].'</td>
<td> '.$row["nomU"].'</td>
<td> '.$row["email"].'</td>
<td> '.$row["Contact"].'</td>
<td>' ?>
<button type="button" class="btn btn-success valider">Valider</button> /<button type="button" class="btn btn-danger rejeter">Rejeter</button>
<?php
echo '</td>
<td>'
?>
<input type="hidden" class="the_id" value='<?php echo $row[' id ']; ?>'>
<?php echo '</td>
</tr>';
}
}
else
{
echo "0 results";
}
?>
</tbody>
</div>
</table>
</div>
And I want to show the id of each row when I click on the button "valider" for the line with jquery but it's showing the first id for all the row.
Here is th jquery code
$(document).ready(function(){
$('.valider').click(function(e){
alert($('.the_id').val());
});
});
Almost right. Remember that when accessing by a class name rather than an ID, you will get all of those objects.
So, we need to be more specific. Give this a try:
$('.valider').click(function(e){
var id = $(this).parent().parent().find('td').find('.the_id').val();
alert(id);
});
This takes you from $(this), your button, to its parent <td>, then the parent <tr>, then looks for any classes of .the_id inside the tr, in other words, the specific one you want.
I usually add a custom attribute to the elements, like data-custom-id.
'<button type="button" class="btn btn-success valider" data-custom-id="'.$row["id"].'">Valider</button>'
And then in the calling function I can access the object with this
$('.valider').click(function(e){
alert($(this).attr('data-custom-id'));
});
You need to go up to the parent tr to get the related input so you could use closest() with the $(this) :
$('.valider').click(function(e){
alert( $(this).closest('tr').find('.the_id').val() );
});

Codeigniter load view with data after button submit

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

Update content in HTML table using PHP and AJAX

I am trying to update the contents of the table by pressing the update button in PHP using the MySQL connection. I want to combine Ajax and PHP, because by using that I don't have to refresh the page every time.
I don't have any idea to do this. And I haven't found anything on the internet. So I came here to ask.
Here you see a picture of how my current page looks like.
My table
The code I use to generate/populate the table:
<div class="col-md-12">
<h3 class="title">Active Tasks</h3>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Task</th>
<th>Predecessor</th>
<th>Dev</th>
<th>MOSCOW</th>
<th>Plan</th>
<th>Do</th>
<th>Check</th>
<th>Act</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$query = mysqli_query($conn, $SQL);
while ($row = mysqli_fetch_array($query)) {?>
<tr class="active">
<td style="display:none;><?php echo $row['ID'];?></td>
<td style="display:none;"><?php echo $row['ProjectID'];?></td>
<td><?php echo $row['Task'];?></td>
<td><?php echo $row['Predecessor'];?></td>
<td><?php echo $row['Dev'];?></td>
<td><?php echo $row['MOSCOW'];?></td>
<td class="js-Task-Plan"><?php echo $row['Plan'];?></td>
<td class="js-Task-Do"><?php echo $row['Do'];?></td>
<td><?php echo $row['Check'];?></td>
<td><?php echo $row['Act'];?></td>
<td>
<button type="button" class="js-TimerStart btn btn btn-default">Start</button>
<button type="button" class="js-TimerPauzeer btn btn-default">Pauzeer</button>
<button type="button" class="js-TimerResume btn btn-default">Resume</button>
<a class="btn btn-default btn-info" href="editTask.php?TaskID=<?php echo $row['ID'];?>&projectid=<?php echo $ID;?>">Aanpassen</a>
<a class="btn btn-default btn-danger" href="delete_task.php?TaskID=<?php echo $row['ID'];?>&projectid=<?php echo $ID;?>">Verwijderen</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<button class="btn btn-block btn-default btn-success" name="UpdateTable"><span style="font-weight: bold;">Update</span></button>
</div>
Thank you for every bit of response and information.
Its not too big issue.
You have to just give some class and ids to your table row and tds.
first of all give unique ids to all rows, in your case you have
<?php echo $row['ID'];? and give same class to all rows these will use later steps.
and give data-id to each row with unique id...so
every tr may be named with <tr id='tr_<?php echo $row['ID'];?>' class='projects' data-id='<?php echo $row['ID'];?>'> and Predecessor td witl be <td id='p_<?php echo $row['ID'];?>'></td>
now i am asssuming that you want to update each project Predecessor every 3 seconds..then just you need to write ajax call for updation.
setInterval(function(){
$('.project').each(function(){
var id=$(this).data('id');
$.ajax({
url:'php_file _name_that_takes_project_id_and_get_details_from_database',
data:{id:id},
type:'POST',
sucess:function(data){
// here data is returned value by ajax php file call which takes project id and fetch Predecessor related to this id.
$('#p_'+id).html(data);
}
});
});
, 3000);
Put it in separate script:
<?php
$query = mysqli_query($conn, $SQL);
while ($row = mysqli_fetch_array($query)) {?>
<tr class="active">
<td style="display:none;><?php echo $row['ID'];?></td>
<td style="display:none;"><?php echo $row['ProjectID'];?></td>
<td><?php echo $row['Task'];?></td>
<td><?php echo $row['Predecessor'];?></td>
<td><?php echo $row['Dev'];?></td>
<td><?php echo $row['MOSCOW'];?></td>
<td class="js-Task-Plan"><?php echo $row['Plan'];?></td>
<td class="js-Task-Do"><?php echo $row['Do'];?></td>
<td><?php echo $row['Check'];?></td>
<td><?php echo $row['Act'];?></td>
<td>
<button type="button" class="js-TimerStart btn btn btn-default">Start</button>
<button type="button" class="js-TimerPauzeer btn btn-default">Pauzeer</button>
<button type="button" class="js-TimerResume btn btn-default">Resume</button>
<a class="btn btn-default btn-info" href="editTask.php?TaskID=<?php echo $row['ID'];?>&projectid=<?php echo $ID;?>">Aanpassen</a>
<a class="btn btn-default btn-danger" href="delete_task.php?TaskID=<?php echo $row['ID'];?>&projectid=<?php echo $ID;?>">Verwijderen</a>
</td>
</tr>
<?php } ?>
and call it 'table.php'.
and add js code, which will be make AJAX call to table.php and update our table
$.ajax('table.php', {
success: function(data) {
$('.table-bordered.table-condensed tbidy').html($(data));
$('#notification-bar').text('The page has been successfully loaded');
},
error: function() {
$('#notification-bar').text('An error occurred');
}
});

PHP - DataTables Post username from table row for a mysql_query?

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;

Categories