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>
Related
I want to give extra data of my card in modal where I'm trying to access card id and their data in the modal of the same card.
but I tried the same PHP code into modal but it gave me data of every id in one modal just because I didn't specify the accurate id.
This is my card code:
<?php
$postquery="SELECT * FROM card";
$run=mysqli_query($db,$postquery);
?>
<section>
<div class="container1">
<?php
while($card=mysqli_fetch_assoc($run)){
?>
<div class="container__card">
<div class="container__card--content">
<img src="../images/<?=getcardimagesinfo($db,$card['id'])?>" style="width:100%;
height:100px !important"/>
<h3><?=$card['Name']?></h3>
<h3><?=$card['id']?></h3>
<button type="button" id="modalss" class="a modalss" data-bs-toggle="modal" data-bs-
target="#exampleModal">Start</button>
</div>
</div>
<?PHP
}
?>
</div>
This one is my modal
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-
hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title text-light" id="exampleModalLabel">Card full Information</h5>
<button type="button" class="btn-close" data-id data-bs-dismiss="modal" aria-
label="Close"></button>
</div>
<div class="modal-body" style="color:white;">
<?PHP
while($card=mysqli_fetch_assoc($run)){
?>
<h6>Description: <?=$card['Description']?></h6>
<h6>Hint: <?=$card['hints']?></h6>
<?php
}
?>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-secondary">Save</button>
</div>
</div>
</div>
</div>
First of all render all of your cards:
<?php
$postquery="SELECT * FROM card";
$run=mysqli_query($db,$postquery);
$allCards = [];
while($card=mysqli_fetch_assoc($run))
{
$allCards[] = $card;
}
?>
<section>
<div class="container1">
<?php
foreach($allCards as $card){
$cardId = $card['id'];
$img = getcardimagesinfo($db,$cardId);
?>
<div class="container__card">
<div class="container__card--content">
<img src="../images/<?=$img?>" style="width:100%; height:100px !important" alt="Image of card"/>
<h3><?=$card['Name']?></h3>
<h3><?=$card['id']?></h3>
<button id="card-<?=$cardId?>" type="button" class="a modalss"
onclick="updateModalContent(this)"
data-bs-toggle="modal" data-bs-target="#exampleModal">
Start
</button>
</div>
</div>
<?php } ?>
</div>
</section>
Then embed your modal at the bottom of your page content.
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title text-light" id="exampleModalLabel">Card full Information</h5>
<button type="button" class="btn-close" data-id data-bs-dismiss="modal" aria-
label="Close"></button>
</div>
<div class="modal-body" style="color:white;">
<h6>Description: <span id="modal-card-description"></span></h6>
<h6>Hint: <span id="modal-card-hint"></span></h6>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-secondary">Save</button>
</div>
</div>
</div>
</div>
Finally add JavaScript section at the bottom to handle updating data in the modal window:
<script>
// This contains all cards info as Json object in javascript when the page is loaded
var allCards = <?php echo json_encode($allCards);?>;
// Button click handler
function updateModalContent(clickedButton)
{
let cardId = clickedButton.id.split("-")[1];
let thisCardInfo = allCards[cardId];
document.getElementById('modal-card-description').innerHTML = thisCardInfo.description;
document.getElementById('modal-card-hints').innerHTML = thisCardInfo.hints;
// and so on ....
}
</script>
I'm using modal from bootstrap. The modal opens but it doesn't close either with the cross or close button.
This is what I have on my index.php file:
<div id="editar" class="modal fade" role="dialog" tabindex="-1" aria-hidden="true">
</div>
And this is the modal which is in a separated .php file:
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Editar Accion</h4>
<button type="button" class="close" data-dismiss="modal" id="cerrar">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input type="hidden" name="editar" value="<?php echo $id; ?>">
<div class="form-group">
<label for="item_name">Accion:</label>
<input type="text" class="form-control" id="accion" value="<?php echo $accion; ?>">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary mb-3" name="update" id="btn-editar"><span class="glyphicon glyphicon-edit"></span> Editar</button>
<button type="button" class="btn btn-secondary mb-3" data-dismiss="modal"><span class="glyphicon glyphicon-remove-circle"></span> Cancelar</button>
</div>
</div>
</div>
I have to keep them in two files.
If you noticed my comment, I have told you its headache to use two different pages. Also it is not recommended way.
I have created a button for you and on the button click the modal shows up with cancel button that goes away when you click it. It is the way exactly you wanted afterwards.
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Click Me
</button>
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Editar Accion</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<input type="hidden" name="editar" value="<?php echo $id; ?>">
<div class="form-group">
<label for="item_name">Accion:</label>
<input type="text" class="form-control" id="accion" value="<?php echo $accion; ?>">
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-primary mb-3" name="update" id="btn-editar"><span class="glyphicon glyphicon-edit"></span> Editar</button>
<button type="button" class="btn btn-secondary mb-3" data-dismiss="modal"><span class="glyphicon glyphicon-remove-circle"></span> Cancelar</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Hope this helps.
I think it is better to make use of JQuery so that you can have more control over what happens (e.g: clear fields, etc) on the modal on close.
$(document).on('click','#button_id',function(e){
e.preventDefault();
//you can clear fields first if you need to
$('#accion').val('');
$('#modal_id').hide(); //close modal
});
Please remember to include the file into the index.php. Keeping modal in separate file will help you reuse the same somewhere in your code, that is if you keep your modal code dynamic enough.
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 } ?>
I have a form that I submit using a bootstrap button, I have a modal that I link to in order to print using another button. I want the printer button to save the form like the submit button does, but instead of redirecting to another page, it should redirect to the print modal (So the print contents are up to date).
This is my code I have that submits/prints currently. If I make changes to a loaded form, the print button will only print the prior form.
<!-- Save Project -->
<div class='form-group text-center'>
<input data-loading-text="Saving Project..." type="submit" name="invoice_btn" value="Save Project" class="btn btn-success submit_btn invoice-save-btm" />
<!-- Print Project -->
<div style="position:absolute; left:500px; top:200px;"><a title="Print Invoice" class="print_class" role="menuitem" tabindex="-1" data-uuid="<?php echo isset($invoice['Invoice']['uuid']) ? nl2br( $invoice['Invoice']['uuid'] ): ''; ?>" href="javascript:;"> <i class="fa fa-print fa-4x"></i><br>Print</a></div>
</div>
</div>
Modal Print :
<!-- Modal Print -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" style="width:900px;height:auto">
<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" id="myModalLabel">Print Proposal</h4>
</div>
<div class="modal-body" id="print_content">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="printBtn" onclick="printInvoice('print_content')" class="btn btn-primary" data-loading-text="Printing...">Print</button>
</div>
</div>
</div>
</div>
The code below shows the user's recent posts from the database, and a delete button if the user viewing the post
<div class="span12">
<?php
// get current user ID
$userid = $row['0'];
// get posts
$sql_posts = "SELECT * FROM posts WHERE ownerid='$userid' ORDER BY id DESC LIMIT 0,5";
$result_posts = mysql_query($sql_posts);
// for each post, show le post.
while($row_posts = mysql_fetch_assoc($result_posts)) {
?>
<div class="well">
<span class="label"><?php echo date('F j Y',strtotime($row_posts['time']));?></span>
at
<span class="label"><?php echo date('g:i a',strtotime($row_posts['time']));?></span>
<?php
if($player==$_SESSION['username']) {
?>
<a href="#deletepost" data-toggle="modal">
<span class="label label-important">Delete post</span>
</a>
<!-- delete post modal -->
<div id="deletepost" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close delete" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Delete post</h3>
</div>
<div class="modal-body">
Are you want to delete post #<?php echo $row_posts['id'];?>?
<p class="muted">
"<i><?php echo $row_posts['contents'];?></i>"
</p>
</div>
<div class="modal-footer">
<form class="pull-right form-inline" method="post" action="post_delete.php">
<input type="hidden" value="<?php echo $row_posts['id'];?>" name="postid">
<input type="hidden" value="<?php $filepath = $_SERVER["SCRIPT_NAME"]; echo basename($filepath);?>" name="currentpage">
<button type="button" class="btn" data-dismiss="modal" aria-hidden="true">Keep the post</button>
<button type="submit" class="btn btn-danger">I am sure. Delete the post!</button>
</form>
</div>
</div>
<!-- end modal -->
<?php
} // end delete post button
?>
<hr width="250px">
<img src="profilepic.php?player=<?php echo $player;?>&size=32" />
<?php echo $row_posts['contents'];?>
</div>
<?php
} // end post foreach
?>
</div>
For some reason, once the user hits the modal it shows the same post every time. For example, if the user hit delete on the first post and the post contents was hello, it would show hello in the modal. However, for all the rest of the posts in the loop if you hit Delete it will show the first post in every single modal.
Your href for your delete link is, #deletepost, and your modal ID is always the same.
Change it so it doesn't use ID's, or make each ID different.
Try this,
<div class="span12">
<?php
// get current user ID
$userid = $row['0'];
// get posts
$sql_posts = "SELECT * FROM posts WHERE ownerid='$userid' ORDER BY id DESC LIMIT 0,5";
$result_posts = mysql_query($sql_posts);
// for each post, show le post.
while($row_posts = mysql_fetch_assoc($result_posts)) {
?>
<div class="well">
<span class="label"><?php echo date('F j Y',strtotime($row_posts['time']));?></span>
at
<span class="label"><?php echo date('g:i a',strtotime($row_posts['time']));?></span>
<?php
if($player==$_SESSION['username']) {
?>
<a href="#deletepost-<?php echo $row_posts['id'];?>" data-toggle="modal">
<span class="label label-important">Delete post</span>
</a>
<!-- delete post modal -->
<div id="deletepost-<?php echo $row_posts['id'];?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close delete" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Delete post</h3>
</div>
<div class="modal-body">
Are you want to delete post #<?php echo $row_posts['id'];?>?
<p class="muted">
"<i><?php echo $row_posts['contents'];?></i>"
</p>
</div>
<div class="modal-footer">
<form class="pull-right form-inline" method="post" action="post_delete.php">
<input type="hidden" value="<?php echo $row_posts['id'];?>" name="postid">
<input type="hidden" value="<?php $filepath = $_SERVER["SCRIPT_NAME"]; echo basename($filepath);?>" name="currentpage">
<button type="button" class="btn" data-dismiss="modal" aria-hidden="true">Keep the post</button>
<button type="submit" class="btn btn-danger">I am sure. Delete the post!</button>
</form>
</div>
</div>
<!-- end modal -->
<?php
} // end delete post button
?>
<hr width="250px">
<img src="profilepic.php?player=<?php echo $player;?>&size=32" />
<?php echo $row_posts['contents'];?>
</div>
<?php
} // end post foreach
?>
</div>
Use different model ID 's in each time when you call model,
<div class="modal fade" id="<?php echo $id; ?>" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Product</h4>
</div>
<div class="modal-body">
<p>Are you sure, want to delete this?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<span class="btn btn-danger" >Delete</span>
</div>
</div>
</div>
</div>