PHP HTML multiple id of form into modal body - php

I have a table showing the summary for every ticket submitted from user.
Every line of this table has a "Charge Exp" button, that opens a modal that allows to load the expenses for every ticket opened from the user.
<table id="ticketList" class="table table-striped table-hover">
<thead>
<!-- header fields -->
</thead>
<tbody>
<?php
if($numrows_ticket > 0 ) {
if($numrows_ticket < $limit){
$limit = $numrows_ticket;
}
for($i = $paginationStart; $i < ($paginationStart+$limit);$i++){
?>
<tr>
<td><?php echo $arr[$i]['jira_id'];?></td>
<td><?php echo $arr[$i]['project'];?></td>
<td><?php echo date_format(date_create($arr[$i]['data']), "d/m/y H:i");?></td>
<td><?php echo $arr[$i]['id_user'];?></td>
<td><?php echo "xXstatusXx";?></td>
<td><button id="btnExp" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#chargeExpenses"><?php echo "Charge Exp"?></button>
<!-- Modal -->
<div class="modal" id="chargeExpenses" tabindex="-1" role="dialog" aria-labelledby="expensesForm" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header"></div>
<div class="modal-body">
<?php include "load_expenses.php";?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
When I write the data into the fields of the modal (without submitting), the same content appears if I close, go to a different line and open a new modal. When I submit, multiple forms for every line of the table are submitted.
load_expenses.php:
<?php error_log("counter load exp: ".$i);?>
<form action="xxx.php" method="post" id="exp_form-<?php echo $i?>" class="spellcheck exclusive save" name="dett2-<?php echo $i?>" enctype="multipart/form-data">
In the log I have $i from 0 to 9, as I'd expect. The id and name of the form is however always 0.
What's wrong and what could be the best way to do it?

you have only one modal and whenever you add something to it it stays there just gets hide when you close the modal .
There can be some options to do it one of them can be that you use modal id different for each modal like
<div class="modal" id="chargeExpenses[$x]" tabindex="-1" role="dialog" aria-labelledby="expensesForm" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header"></div>
<div class="modal-body">
<?php include "load_expenses.php";?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
and when you open up the modal just pass the id of the row you clicked

Related

bootstrap modal inside a php while loop to display different infornmation

so i have a while loop and i am fethcing the name and the description of them from my db
so when ever i click on one of the parts i want the modal to display the name of the item that i clicked on in the modal i hope this picture would describe it better
below is the code i have so far
at the moment when i click on the modal i get displayed the name of the item which is first on the list no matter where i click
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#mymodal">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="mymodal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
You are not doing things the way you have to do. You are re-creating your mymodal for every iteration of your while loop which is not a better way to achieve what you want.
Follow these steps:
Create your mymodal outside of your while loop.
Pass the ID of current row to a javascript function and populate the data of that id using javascript.
I have set the things which needs to be done. Try the following code
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--- display table header row-->
<table style="width:100%">
<tr>
<th>name</th>
<th>description</th>
<th>Action</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description'];
$id = $row['id']; // I am assuming that your table has auto incremented primary key column by the name of id, if not then replace that by $row['your_id_column']
?>
<!--- desplay the data in a table row -->
<tr>
<td id="<?php echo "name_".$id; ?>"><?php echo $name; ?></td>
<td id="<?php echo "description_".$id; ?>"><?php echo $description ; ?></td>
<td><div href="#" data-toggle="modal" data-target="#mymodal" onClick="showModal(<?php echo $id ; ?>)">Edit</div></td>
</tr>
<?php } ?>
</table>
<!--- Your modal -->
<div id="mymodal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p id="name"></p> <!--- name will be shown here-->
<p id="description"></p><!--- description will be shown here-->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
function showModal(id)
{
// setting the values of clicked item in the bootstrap Modal
$("#name").text($("#name_"+id).text());
$("#description").text($("#description_"+id).text());
}
</script>
Looks like you are not serializing the modals... If you wanted to do things that way your code should be something like;
<!-- language-all: lang-html -->
while($row = mysqli_fetch_assoc($result)) {
$procuct_id = $row['ID'];
$name = $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#mymodal_<?php echo $procuct_id;?>">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="mymodal_<?php echo $procuct_id;?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php }
<--
A Better way to do it would be to write a javascript function to show the generic modal window, then run an ajax call to load your info into the "modal-body" div. You would have to pass a unique id to the call in order to have your ajax script hit the db and get the info to display.
It's been almost 4 years but I'm writing for everyone; change data-target(id) as row id.
edited code:
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#<?php echo $row['id']; ?>">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="<?php echo $row['id']; ?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>

Im getting the wrong id on my view, im getting id number:1 always on my category id

So this is my code, its a Laravel .blade.php but anyway my problem is with this HTML / PHP code.
The problem is that when I press on any delete button I get the same id, I get cat->id = 1. when I press the 'delete' button on the laptop row, I should get in my delete confirmation the id number: 2, but I still get the number: 1 and its that way with any other row
<div class="">
<div class="box">
<div class="box-header">
<h3 class="box-title">All Categories</h3>
</div>
<div class="box-body">
<table class="table table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Id</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
#foreach($categories as $cat)
<tr>
<td>{{$cat->nombre}}</td>
<td>{{$cat->id}}</td>
<td>
<button class="btn btn-danger" data-catid={{$cat->id}} data-toggle="modal" data-target="#delete">Delete</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
For the modal im using the next code:
<div class="modal modal-danger fade" id="delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title text-center" id="myModalLabel"><span>Delete Confirmation</span><</h4>
</div>
<form action="{{route('categories.destroy','test')}}" method="post">
{{method_field('delete')}}
{{csrf_field()}}
<div class="modal-body">
<p class="text-center">
<span>Are you sure you want to delete this {{ $cat->id}}?</span>
</p>
<input type="hidden" name="category_id" id="cat_id" value="">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" data-dismiss="modal"><span>No, Cancel</span></button>
<button type="submit" class="btn btn-warning"><span>Yes, Delete</span></button>
</div>
</form>
</div>
</div>
I pressed the delete button on the Laptop row, and I get the id number:1 when I should get id number:2. this apply on every row
This is because everytime you click on the delete button it calls the same model with the same ID. Change you Button ID
<button class="btn btn-danger" data-catid={{$cat->id}} data-toggle="modal" data-target="#delete-{{$cat->id}}">Delete</button>
And the modal ID as well
<div class="modal modal-danger fade" id="delete-{{$cat->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
NOTE: You might need to keep the modal box in foreach loop so that delete button is mapped to the correct modal box

how to GET value into bootstrap modal

I have a table "on php page" that contain records from a database. At each row there is a button and when I click on the button, a modal bootstrap should appear that contain details about members that you want to know.
This is the button code:
<td> <?php echo '
details'; ?> </td>
and the modal bootstrap code:
<?php
$connect = mysqli_connect('127.0.0.1','root','','smart');
$connect->set_charset("utf8");
if(isset($_GET['id'])){
$Mid = (int)$_GET['id'];
$getid = "select * from members where id = '$Mid'";
$runid = mysqli_query($connect, $getid);
$row_id = mysqli_fetch_array($runid);
$show_name = $row_id['name'];
$show_number = $row_id['number'];
?>
<div class="modal fade bd-example-modal-lg" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"> اضافة فاتورة</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<form action="" method="post">
<label>الاسم</label>
<input type="textbox" name="" class="Mname" disabled value="<?php echo $show_name ?>"></input>
<label>الرقم</label>
<input type="textbox" name="" class="Mname" disabled value="<?php echo $show_number; ?>"></input>
<label>النوع</label>
<select name="dbType" id="dbType">
<option value="normal">عادي</option>
<option value="uargent">مستعجل</option>
</select>
<div id="otherType" style="display:none;">
<label for="specify">Specify</label>
<input type="text" name="specify" placeholder="Specify Databse Type"/>
</div>
</form>
</div>
</div>
</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>
<?php }else{echo'select an row please';} ?>
<script>
$("#myModal").modal("show");</script>
<script src="/our/js/jquery.min.js"></script>
<script src="/our/js/bootstrap.min.js"></script>
<script src="/our/js/run.js"></script>
</body>
But when I click on the add button the modal doesn't appear but the url changes like this:
http://localhost/our/dashboard.php#myModal?id=2
Why doesn't the modal work?
1. You have data-toggle='modal' but it doesn't know what modal it should open. Add this to your button.
data-target='#myModal'
Source
2. This will also trigger the modal.
$("#myModal").modal("show");
You can't use click function in a link that redirect you.
Make normal link with only href attr.
<?php echo 'Button'; ?>
url should be dashboard?id=1
html
<?php if(isset($_GET["id"])) { ?>
<div class="modal fade bd-example-modal-lg" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<!---Your same modal from your question--->
</div>
<?php } ?>
add this before your </body> closing body tag
<script>$("#myModal").modal("show");</script>

Creating Dynamic Modal popups with information from job

I have a PHP loop that renders results from a Database:
<?php
foreach ($results as $result){
$job_numb = $result['job_numb'];
$job_name = $result['job_name'];
$comments = $result['comments'];
?>
<tr>
<td><?php echo "$job_numb";?> </td>
<td><?php echo "$job_name";?></td>
<td> <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#Comments-<?php echo $job_numb;?>">Comments</button>
<!-- Modal -->
<div id="Comments-<?php echo $job_numb;?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Comments for <?php echo "$job_name" . "$job_numb";?></h4>
</div>
<div class="modal-body">
<?php echo "$job_name";?>
<?php echo "$comments";?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</td>
<td>Edit Record</td>
</tr>
<?php
}
?>
The Data target for the modal cannot be a STATIC ID because then it's repeating IDs on the same page - so you will just repeat the same information as the first instance of the ID.
What I am after is to make sure the ID is always different for as many records as there are. I have seen the code before, but I cannot find it now. What am I missing?
Create a unique ID using <?php echo and a unique ID:
<button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#Comments-<?php echo $job_numb;?>">Comments</button>
<div id="Comments-<?php echo $job_numb;?>" class="modal fade" role="dialog">
<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">Comments for <?php echo "$job_name" . " | " . "$job_numb";?></h4>
</div>
<div class="modal-body">
<?php echo "$comments";?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
Using this, I was able to pass all of that dynamic row's specific data to the modal without ID conflicts. I hope this is useful to others seeking out the answer.

Bootstrap: Trouble using modal for showing dynamic data

I have a list created dinamically using php
while($row = $result->fetch_assoc()){ //$row is a row fetched from my database
<tr>
<td><?php echo $row['id']?></td>
<td><?php echo $row['name']?></td>
<td><?php echo $row['surname']?></td>
<td><a href="#Modal" <?php echo "id= " . $row['username'] ?> class="btn btn-small btn-info" data-toggle="modal" > Vedi</a></td>
</tr>
}
Modal is something like this:
<div id="Modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
<div class="modal-header">
<a class="close" data-dismiss="modal" aria-hidden="true">×</a>
<h3 id="myModalLabel">user information</h3>
</div>
<div class="modal-body" id="loadInfo">
<div class="row-fluid">
<div class="span12">
<!-- my php code here -->
</div>
</div>
</div>
</div>
My php code must be referred to a precise user. So I need that the Modal can receive this the id of the in order to execute the proper php code.
How can I obtain this result?
assign the values of the user info to javascript variables. Add a data attribute to the modal toggle buttons like data-user_id=<?php echo $row['id']; ?> and make a jquery click handler to update the modal's content to match the button you clicked.

Categories