I know here are some similar Topics but I couldn't find a solution for my problem.
I have a table where I have a delete button for every entry (in code called "wp"). When I click this button a modal opens. In this model, I want to Show some data for which I Need to pass the ID of the item I want to delete to the Controller, where I pass it to a function which includes a database query and finally Needs to get the right value back to the view.
View:
<tbody>
<?php
if($wps!= NULL){
foreach($wps as $wp){
?>
<tr>
<td><?php echo $wp->type; ?></td>
<td>....</td>
<a class="btn btn-default btn-icon" title="Edit" href="<?php echo site_url("myurl"); ?>"><i class="icon icon-edit" aria-hidden="true"></i></a>
<a class="btn btn-default btn-icon" title="Create" href="<?php echo site_url("myurl"); ?>"><i class="icon icon-add" aria-hidden="true"></i></a>
<button value="<?php echo $wp->id; ?>" class="btn btn-default btn-icon delete" title="Delete" data-toggle="modal" data-target="#modal-delete-<?php echo $wp->id; ?>" type="button"><i class="icon icon-move-to-trash" aria-hidden="true"></i></button>
</td>
</tr>
<div id="modal-delete-<?php echo $wp->id; ?>" class="modal fade" aria-labelledby="exampleModalLabel2" aria-hidden="true" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">x</span>
<span class="sr-only">Close</span>
</button>
<div class="modal-header">
<h4 class="modal-title" id="exampleModalLabel2"><strong>Delete </strong><br><br>Account: <strong><?php echo $cus->name ?></strong><br>: <strong><?php echo $wp->type ?></strong><br>Service in Scope: <strong><?php echo $wp->sis?></strong><br>This cannot be undone. Do you want to continue?</h4>
<ul class="content-list">
<?php
if($finding != NULL){
foreach($finding as $f){
?>
<ul class="content-list">
<li class="media">
<div class="media-body">
<div class="media-heading"><?php echo $f->sis?></div>
<div class="media-hint"><?php echo $f->finding ?></div>
</div>
</li>
</div>
<?php }} ?>
</ul>
</div>
<div class="modal-footer">
<br>
<center>
<a href="<?php echo site_url("myurl"); ?>" class="btn btn-brand" title="Delete" style="width: 170px;">
<div align="center"><i class="icon icon-confirm" aria-hidden="true"></i> Delete</div>
</a>
<button type="button" class="btn btn-brand" title="Return" data-dismiss="modal" style="width: 170px;">
<div align="center"><i class="icon icon-cancel" aria-hidden="true"></i> Return</div>
</button>
</center>
</div>
</div>
</div>
</div>
<?php }} ?>
</tbody>
Controller:
$data["finding"] = $this->Model->get_findings($needed_value);
It would be great if you have an idea how I can pass the $wp->id from view to controller
Using ajax and Jquery you can pass the values. Anyway you are using modal popup, so its is easy. Check the possibilities of modal popup.
Sending data to a controller. one way is to Do it using a link. This is an example. The $id gives the link to the controller. Then you use the ID from your db as the link(anchor). The $ID could be a link to a controller that deletes the data in the db
<?php foreach($getlist as $row)
{
$id = "forms/fill_form/$row->id";
$this->table->add_row(
anchor($id, $row->id),
$row->title,
$row->date,
$row->parent,
$row->status
);
}
Related
I have a problem with a bootstrap modal. I'm using PHP.
In an admin panel there is a table with the list of the users and the possibility to edit or delete a user's profile. For the delete I want to create a modal for the confirm of the delete but, when I click on "confirm" button inside the modal, the modal gets by default the user ID of the first user in the table and not the user ID of the selected user.
Here is the code:
<?php foreach ($utenti as $utente) { ?>
<tr>
<th scope="row"> <?php echo $utente['idUser']?> </th>
<td><?php echo $utente['nome']." ".$utente['cognome']?></td>
<?php if($_SESSION['role'] == 1) {?>
<td><?php echo $utente['az']?></td>
<?php } ?>
<td><?php echo $utente['email']?></td>
<td class="text-warning"><a
href="<?php echo 'editUser.php?user='.$utente['idUser']?>"><i
class="fas fa-edit text-warning"></i></a></td>
<!-- <td class="text-warning"><i class="fas fa-trash-alt text-danger"></i></td> -->
<td class="text-danger">
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete"><i
class="fas fa-trash-alt text-danger"></i><?php var_dump($utente['idUser']); ?>
</button>
</td>
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
Attenzione <?php var_dump($utente['idUser']); ?></h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Continuando eliminerai l'utente in maniera irreversibile</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger "
><a
class="text-white btn-modal-confirm"
href="<?php echo '?action=delete&user='.$utente['idUser']?>"
>Elimina</a>
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Indietro
</button>
</div>
</div>
</div>
</div>
</tr>
<?php }?>
If I make a var_dump of $utente['idUser'] before the modal, it gets the right user ID. If I make it inside the modal it gets by default the first ID, as I said.
Notice that every modal trigger button has a data-target attribute to define which modal will be opened.
In your case, the button of every row you used to triggered the modal have the same data-target, which is #confirmDelete. These modals behind also has the same id called #confirmDelete, so every time you hit the modal trigger button (all had the same data-target) then eventually it will shows up the very first modal element.
For a better understanding, compare my code to yours and see the differences.
<?php foreach ($utenti as $utente) { ?>
<tr>
<th scope="row"> <?php echo $utente['idUser']?> </th>
<td><?php echo $utente['nome']." ".$utente['cognome']?></td>
<?php if($_SESSION['role'] == 1) {?>
<td><?php echo $utente['az']?></td>
<?php } ?>
<td><?php echo $utente['email']?></td>
<td class="text-warning"><a
href="<?php echo 'editUser.php?user='.$utente['idUser']?>"><i
class="fas fa-edit text-warning"></i></a></td>
<!-- <td class="text-warning"><i class="fas fa-trash-alt text-danger"></i></td> -->
<td class="text-danger">
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete_<?php echo $utente['idUser']; ?>"><i
class="fas fa-trash-alt text-danger"></i><?php var_dump($utente['idUser']); ?>
</button>
</td>
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete_<?php echo $utente['idUser']; ?>">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
Attenzione <?php echo $utente['idUser']; ?></h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Continuando eliminerai l'utente in maniera irreversibile</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger "
><a
class="text-white btn-modal-confirm"
href="<?php echo '?action=delete&user='.$utente['idUser']?>"
>Elimina</a>
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Indietro
</button>
</div>
</div>
</div>
</div>
</tr>
<?php }?>
In the above code, I gave every pair of modal elements (modal trigger button and modal ID) a unique data-target value and a unique element id.
...
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete_<?php echo $utente['idUser']; ?>">
...
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete_<?php echo $utente['idUser']; ?>">
...
Now each pair of modal elements have their own ids and they should be working the way you wanted.
I want to make a modal for delete confirmation, but I can't take the value of the record from the table and pass it to the modal and delete the record.
$query1=mysqli_query($conn,"select * from tab1 where username = '$row[username]'");
while($row =mysqli_fetch_array($query1))
{
echo '
<tr class ="item" id=echo $row["country"]">
<td>'.$row["country"].'</td>
<td>
<a class="Danger danger-color" href="#" data-toggle="modal" data-target="#DangerModalalert">
<i class="glyphicon glyphicon-trash"></i></a>
</td>
<div id="DangerModalalert" class="modal modal-edu-general FullColor-popup-DangerModal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-close-area modal-close-df">
<a class="close" data-dismiss="modal" href="#"><i class="fa fa-close"></i></a>
</div>
<div class="modal-body">
<span class="glyphicon glyphicon-trash fa-2x"></span>
<h2>Delete Confirmation</h2>
<p>Are you sure you want to delete it?</p>
</div>
<div class="modal-footer danger-md">
<a data-dismiss="modal" href="#" style="background:red">No</a>
Yes
</div>
</div>
</div>
</div>
First, you only need one modal div. Add class delete-yes to delete button so we can replace it's href later from javascript
<div id="DangerModalalert" class="modal modal-edu-general FullColor-popup-DangerModal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-close-area modal-close-df">
<a class="close" data-dismiss="modal" href="#"><i class="fa fa-close"></i></a>
</div>
<div class="modal-body">
<span class="glyphicon glyphicon-trash fa-2x"></span>
<h2>Delete Confirmation</h2>
<p>Are you sure you want to delete it?</p>
</div>
<div class="modal-footer danger-md">
<a data-dismiss="modal" href="#" style="background:red">No</a>
<a class="delete-yes" href="#" style="background:red">Yes</a>
</div>
</div>
</div>
</div>
Set url to delete link, and class delete-confirm for JS binding:
<a class="Danger danger-color delete-confirm" href="delete.php?id=<?php echo $row["id"];?>" data-toggle="modal" data-target="#DangerModalalert">
<i class="glyphicon glyphicon-trash"></i>
</a>
then add this JS code to bind delete button event
$(document).on("click", ".delete-confirm", function () {
var deleteUrl = $(this).attr('href');
$(".modal-body .delete-yes").attr('href', deleteUrl);
return false;
});
I'm creating a web back end for a restaurant. There is an option to block/unblock restaurant owners. I have put an "if" condition to appear button (a tag) according to the status of the restaurant owner. Within this "a" tag I want to pass the value (id) to a modal box.
I put some code in the "data-target" of "a" tag, as well as in the modal id. The value didn't pass and also didn't open the modal.
<tbody>
<?php
global $con;
$sql = "SELECT * FROM `fd_owner_details`";
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
$kk=$row['id'];
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fname']. " " .$row['lname'] ; ?></td>
<td><?php echo $row['address_line1']. ", " .$row['address_line2'] ; ?></td>
<td><?php echo $row['contact_no1'].", ".$row['contact_no2']; ?></td>
<td><?php echo '<img src= "'.$row['image'].'">'; ?></td>
<td>
<?php
if ($row['status']==1){
echo "<span class='label mb-2 mb-xl-0 label-dark'>Active</span>";
}
else {
echo "<span class='label mb-2 mb-xl-0 label-light'>Disabled</span>";
}
?>
</td>
<td>
<span>
<i class="fa fa-pencil" aria-hidden="true"></i>
<?php
if ($row['status']==1){
echo '<i class="fa fa-ban" aria-hidden="true"></i>';
}
else {
echo "<a href='#' data-toggle='modal' data-target='#unblockRestaurantOwner' class='btn btn-warning btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Unblock'><i class='fa fa-check' aria-hidden='true'></i></a>";
}
?>
<i class="fa fa-trash" aria-hidden="true"></i>
</span>
</td>
</tr>
<?php } ?>
</tbody>
--------------------------------------------------------------------------------
<div class="modal fade" id="blockRestaurantOwner?id=<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="blockRestaurantOwnerTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Delete </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">× </span>
</button>
</div>
<div class="modal-body">
<p id="main-content">Are you sure you want to block this restaurant owner ???</p>
<p id="content">You will not be able to recover this action !!!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No, Cancel it </button>
<button type="button" class="btn btn-warning">Yes, Block</button>
</div>
</div>
</div>
</div>
Where Should I change, to get the "id" to the modal ?
Just copy the whole script and try to past inside a php file and try to run it. This can be modified as expected at your end using while loop.
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<?php
$array[] = array('id'=>1, 'fname'=>'hari','lname'=>'ltest','address_line1'=>'no:9','address_line1'=>'rajan street','contact_no1'=>'92222','contact_no2'=>'899782','status'=>1);
$array[] = array('id'=>2, 'fname'=>'vvvv','lname'=>'ltest2','address_line1'=>'no:92','address_line1'=>'siv street','contact_no1'=>'522','contact_no2'=>'2922','status'=>0);
?>
<table border=1>
<tbody>
<?php foreach($array as $row){ ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fname']. " " .$row['lname'] ; ?></td>
<td><?php echo $row['address_line1']. ", " .$row['address_line2'] ; ?></td>
<td><?php echo $row['contact_no1'].", ".$row['contact_no2']; ?></td>
<td>
<?php
if ($row['status']==1){
echo "<span class='label mb-2 mb-xl-0 label-dark'>Active</span>";
}
else {
echo "<span class='label mb-2 mb-xl-0 label-light'>Disabled</span>";
}
?>
</td>
<td>
<span>
<i class="fa fa-pencil" aria-hidden="true"></i>
<?php if ($row['status']==1){ ?>
<a data-toggle="modal" href="#blockRestaurantOwner<?=$row['id']?>" class="btn btn-warning btn-xs" data-toggle="tooltip" data-placement="top" data-original-title="Block"><i class="fa fa-ban" aria-hidden="true"></i></a>
<div id="blockRestaurantOwner<?php echo $row['id']; ?>" class="modal fade" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Delete </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">× </span>
</button>
</div>
<div class="modal-body">
<p id="main-content">Are you sure you want to block this restaurant owner ???</p>
<p id="content">You will not be able to recover this action !!!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No, Cancel it </button>
<button type="button" class="btn btn-warning">Yes, Block</button>
</div>
</div>
</div>
</div>
<?php }
else {
echo "<a href='#' data-toggle='modal' data-target='#unblockRestaurantOwner' class='btn btn-warning btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Unblock'><i class='fa fa-check' aria-hidden='true'></i></a>";
}
?>
<i class="fa fa-trash" aria-hidden="true"></i>
</span>
</td>
</tr>
<?php } ?>
</tbody>
</table>
you can set onclick listener to block button and pass owner id to that function like below
<tbody>
<?php
global $con;
$sql = "SELECT * FROM `fd_owner_details`";
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
$kk=$row['id'];
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fname']. " " .$row['lname'] ; ?></td>
<td><?php echo $row['address_line1']. ", " .$row['address_line2'] ; ?></td>
<td><?php echo $row['contact_no1'].", ".$row['contact_no2']; ?></td>
<td><?php echo '<img src= "'.$row['image'].'">'; ?></td>
<td>
<?php
if ($row['status']==1){
echo "<span class='label mb-2 mb-xl-0 label-dark'>Active</span>";
}
else {
echo "<span class='label mb-2 mb-xl-0 label-light'>Disabled</span>";
}
?>
</td>
<td>
<span>
<i class="fa fa-pencil" aria-hidden="true"></i>
<?php
if ($row['status']==1){
echo '<i class="fa fa-ban" aria-hidden="true"></i>';
}
else {
echo "<a href='#' data-toggle='modal' data-target='#unblockRestaurantOwner' class='btn btn-warning btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Unblock'><i class='fa fa-check' aria-hidden='true'></i></a>";
}
?>
<i class="fa fa-trash" aria-hidden="true"></i>
</span>
</td>
</tr>
<?php } ?>
</tbody>
<div class="modal fade" id="blockRestaurantOwner" tabindex="-1" role="dialog" aria-labelledby="blockRestaurantOwnerTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Delete </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">× </span>
</button>
</div>
<div class="modal-body">
<p id="main-content">Are you sure you want to block this restaurant owner ???</p>
<p id="content">You will not be able to recover this action !!!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No, Cancel it </button>
<button type="button" class="btn btn-warning">Yes, Block</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var target_owner_id="";
function set_target_id(id){
target_owner_id = id;
console.log(target_owner_id);
}
</script>
Currently i have a table that is generated via DB and i want to view the details of the selected value on the data table. I separated the modal with view/users/modal. but there's an error
Message: Trying to get property 'user_id' of non-object
please help me to resolve this
I am running Codeigniter 3.1.10
Xampp V3.2.3
Data Table
<?php echo $this->load->view('users/Modal/view_modal'); ?>
<tbody>
<?php if(!empty($value)): ?>
<?php foreach($value as $row): ?>
<tr>
<td align="center"><?php echo $row->user_id; ?></td>
<td align="center"><?php echo $row->firstname; ?></td>
<td align="center"><?php echo $row->lastname; ?></td>
<td align="center"><?php echo $row->email; ?></td>
<td align="center">
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#info_modal<?php echo $row->user_id; ?>">
<i class="far fa-eye"></i>
</button>
<a href="<?php echo base_url('users/view_edit_form/'.$row->user_id); ?>" class="btn btn-success">
<i class="fas fa-user-edit"></i>
</a>
<a href="" class="btn btn-danger">
<i class="fas fa-ban"></i>
</a>
</td>
<?php endforeach; ?>
My Modal
<div class="modal fade" id="info_modal<?php echo $row->user_id; ?> " tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"><?php echo $row->firstname; ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
I expect the output will display more information of the user but the actual output is an error message Message: Trying to get property 'user_id' of non-object
try to check what $row contains.
If -> don't work, try using $row['user_id']
I want pass a variable from php while to include.php inside the bootstrap modal.
My php while is on the whileresult.php
<?php
$i=0;
while ($i < $rows) {
$title_books=mysql_result($result,$i,"title_books");
$pages_books=mysql_result($result,$i,"pages_books");
$author_books=mysql_result($result,$i,"author_books");
?>
<?php echo $title_books; ?>
after, there is a bootstrap modal and this there are a include.php
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="col-md-1">
<div class="btn-group">
<button type="button" class="btn btn-primary btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fa fa-cog fa-2x" aria-hidden="true"></i><span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
Book in library
</li>
</ul>
</div>
</div>
<!-- start modal with include.php -->
<div id="modalShareBk" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Your Books</h4>
</div>
<div class="modal-body">
<?php include("include.php"); ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-xs" data-dismiss="modal">
<i class="fa fa-times-circle" aria-hidden="true"></i> Close
</button>
</div>
</div>
</div>
</div>
Now, how to pass the value of variable $title_books inside the include.php page?
Thanks
Simply echo out the values in the include file the scope of the variable will be there in include.php
PS. Use require_once() instead of include()
Edit 1:
You need to change the whileresult.php code to something like this
whileresult.php
<?php
$i=0;
$title_books = array();
$pages_books = array();
$author_books = array();
while ($i < $rows) {
$title_books[$i]=mysql_result($result,$i,"title_books");
$pages_books[$i]=mysql_result($result,$i,"pages_books");
$author_books[$i]=mysql_result($result,$i,"author_books");
$i++;
?>
include.php
<?php
for($j=0;$j<$i;$j++)
{
echo 'Book - '.$title_books[$j].' Pages - '.$pages_books[$j].'Author - '.$author_books[$j];
}
?>