Load database into Bootstrap modal
I have an employee database from which I load employee images into a bootstrap grid. On the initial page load, I display all employees, and then I click on various buttons to change the views depending on the query.
What I'm stuck on mow is the bootstrap modal that I want to implement.
I currently have the modal appearing with an image and other information like name, work location, etc. However, the modal will only display the first image (and associated information), regardless of which image I click from the main page. My assumption is that I have to do an ajax call to reload data for the modal when I click on an image. I would do a MySQL query to select * from the database where the file = $imageURL.
I've read numerous posts, and this one seems to be what I'm looking for, although I cannot seem to figure it out: bootstrap modal does not load new data from database
This is the code I am currently working from: I've almost got everything working, except for this last part. Any help is appreciated! Thanks
<?php
include ('db_connect.php');
$sql = "SELECT * from monctonfir where 1 ";
if(isset($_GET['specialty'])) $sql .= " AND specialty = '".$_GET['specialty']."' ";
$sql .=" order by initials ASC";
$result = $db->query($sql);
if($result->num_rows > 0){
while ($row = $result->fetch_assoc()){
$imageURL = 'image/'.$row["file"];
$initial = $row["initials"];
$name = $row["name"];
$location = $row["location"];
?>
<!-- Modal -->
<div class="modal fade" id="exampleModal" 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">Employee Profile</h5>
<button type="button" class="close" data-dismiss="modal" aria-
label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<img src="<?php echo "$imageURL" ?>"/>
<p style="text-align: right">Name: <?php echo $name ?></p>
<p style="text-align: right">Location: <?php echo $location ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- End Modal-->
<div id="photos" class = "col-lg-1 no-gutters" style="margin-top:1rem;">
<div class="card" style="width: 6.5rem;">
<a href = "#">
<img id="staff" type="button" class="img card-img-top" data-toggle="modal" data-target="#exampleModal" src = "<?php echo $imageURL ?>">
</a>
<div class = "card-body">
<h5 class = "card-title round-button" style="text-align: center;"><?php echo $initial ?></h5>
</div>
</div>
</div>
<?php }
} ?>
ajax bootstrap-4 bootstrap-modal
|
show 3 more comments
I have an employee database from which I load employee images into a bootstrap grid. On the initial page load, I display all employees, and then I click on various buttons to change the views depending on the query.
What I'm stuck on mow is the bootstrap modal that I want to implement.
I currently have the modal appearing with an image and other information like name, work location, etc. However, the modal will only display the first image (and associated information), regardless of which image I click from the main page. My assumption is that I have to do an ajax call to reload data for the modal when I click on an image. I would do a MySQL query to select * from the database where the file = $imageURL.
I've read numerous posts, and this one seems to be what I'm looking for, although I cannot seem to figure it out: bootstrap modal does not load new data from database
This is the code I am currently working from: I've almost got everything working, except for this last part. Any help is appreciated! Thanks
<?php
include ('db_connect.php');
$sql = "SELECT * from monctonfir where 1 ";
if(isset($_GET['specialty'])) $sql .= " AND specialty = '".$_GET['specialty']."' ";
$sql .=" order by initials ASC";
$result = $db->query($sql);
if($result->num_rows > 0){
while ($row = $result->fetch_assoc()){
$imageURL = 'image/'.$row["file"];
$initial = $row["initials"];
$name = $row["name"];
$location = $row["location"];
?>
<!-- Modal -->
<div class="modal fade" id="exampleModal" 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">Employee Profile</h5>
<button type="button" class="close" data-dismiss="modal" aria-
label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<img src="<?php echo "$imageURL" ?>"/>
<p style="text-align: right">Name: <?php echo $name ?></p>
<p style="text-align: right">Location: <?php echo $location ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- End Modal-->
<div id="photos" class = "col-lg-1 no-gutters" style="margin-top:1rem;">
<div class="card" style="width: 6.5rem;">
<a href = "#">
<img id="staff" type="button" class="img card-img-top" data-toggle="modal" data-target="#exampleModal" src = "<?php echo $imageURL ?>">
</a>
<div class = "card-body">
<h5 class = "card-title round-button" style="text-align: center;"><?php echo $initial ?></h5>
</div>
</div>
</div>
<?php }
} ?>
ajax bootstrap-4 bootstrap-modal
1
You are repeating your modal code inside the while loop, if you want the modal to refresh with new data you only need 1 modal box, not 1 for every user (although this is possible). The problem comes down to not having unique IDs
– Second2None
Jan 2 at 0:54
1
You don't need the php variables for the modal yet, you need to make an AJAX call to a PHP file that returns those variables for you to fill in the modal. Your modal should be outside the while loop. You also need to make sure when you are creating your html, all elements have a unique ID, if you take your modal out of the while loop you still have repeating IDs which will cause errors later on for you - <div id="photos", <img id="staff" etc.
– Second2None
Jan 2 at 1:10
1
Ideally you should have an ID column in your DB that allows you to reference each specific row and can be used to set IDs for html elements that reference that particular row.
– Second2None
Jan 2 at 1:10
1
It's normally easier to create a new PHP file for ajax calls and to return your results in JSON. Yes you would write your SQL query for retrieving users info in that file. All you would need to send is the ID and retrieve that specific row and the required columns to populate the modal fields.
– Second2None
Jan 2 at 1:21
1
You can use POST or GET that is up to you but generally speaking you should use POST.
– Second2None
Jan 2 at 1:22
|
show 3 more comments
I have an employee database from which I load employee images into a bootstrap grid. On the initial page load, I display all employees, and then I click on various buttons to change the views depending on the query.
What I'm stuck on mow is the bootstrap modal that I want to implement.
I currently have the modal appearing with an image and other information like name, work location, etc. However, the modal will only display the first image (and associated information), regardless of which image I click from the main page. My assumption is that I have to do an ajax call to reload data for the modal when I click on an image. I would do a MySQL query to select * from the database where the file = $imageURL.
I've read numerous posts, and this one seems to be what I'm looking for, although I cannot seem to figure it out: bootstrap modal does not load new data from database
This is the code I am currently working from: I've almost got everything working, except for this last part. Any help is appreciated! Thanks
<?php
include ('db_connect.php');
$sql = "SELECT * from monctonfir where 1 ";
if(isset($_GET['specialty'])) $sql .= " AND specialty = '".$_GET['specialty']."' ";
$sql .=" order by initials ASC";
$result = $db->query($sql);
if($result->num_rows > 0){
while ($row = $result->fetch_assoc()){
$imageURL = 'image/'.$row["file"];
$initial = $row["initials"];
$name = $row["name"];
$location = $row["location"];
?>
<!-- Modal -->
<div class="modal fade" id="exampleModal" 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">Employee Profile</h5>
<button type="button" class="close" data-dismiss="modal" aria-
label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<img src="<?php echo "$imageURL" ?>"/>
<p style="text-align: right">Name: <?php echo $name ?></p>
<p style="text-align: right">Location: <?php echo $location ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- End Modal-->
<div id="photos" class = "col-lg-1 no-gutters" style="margin-top:1rem;">
<div class="card" style="width: 6.5rem;">
<a href = "#">
<img id="staff" type="button" class="img card-img-top" data-toggle="modal" data-target="#exampleModal" src = "<?php echo $imageURL ?>">
</a>
<div class = "card-body">
<h5 class = "card-title round-button" style="text-align: center;"><?php echo $initial ?></h5>
</div>
</div>
</div>
<?php }
} ?>
ajax bootstrap-4 bootstrap-modal
I have an employee database from which I load employee images into a bootstrap grid. On the initial page load, I display all employees, and then I click on various buttons to change the views depending on the query.
What I'm stuck on mow is the bootstrap modal that I want to implement.
I currently have the modal appearing with an image and other information like name, work location, etc. However, the modal will only display the first image (and associated information), regardless of which image I click from the main page. My assumption is that I have to do an ajax call to reload data for the modal when I click on an image. I would do a MySQL query to select * from the database where the file = $imageURL.
I've read numerous posts, and this one seems to be what I'm looking for, although I cannot seem to figure it out: bootstrap modal does not load new data from database
This is the code I am currently working from: I've almost got everything working, except for this last part. Any help is appreciated! Thanks
<?php
include ('db_connect.php');
$sql = "SELECT * from monctonfir where 1 ";
if(isset($_GET['specialty'])) $sql .= " AND specialty = '".$_GET['specialty']."' ";
$sql .=" order by initials ASC";
$result = $db->query($sql);
if($result->num_rows > 0){
while ($row = $result->fetch_assoc()){
$imageURL = 'image/'.$row["file"];
$initial = $row["initials"];
$name = $row["name"];
$location = $row["location"];
?>
<!-- Modal -->
<div class="modal fade" id="exampleModal" 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">Employee Profile</h5>
<button type="button" class="close" data-dismiss="modal" aria-
label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<img src="<?php echo "$imageURL" ?>"/>
<p style="text-align: right">Name: <?php echo $name ?></p>
<p style="text-align: right">Location: <?php echo $location ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- End Modal-->
<div id="photos" class = "col-lg-1 no-gutters" style="margin-top:1rem;">
<div class="card" style="width: 6.5rem;">
<a href = "#">
<img id="staff" type="button" class="img card-img-top" data-toggle="modal" data-target="#exampleModal" src = "<?php echo $imageURL ?>">
</a>
<div class = "card-body">
<h5 class = "card-title round-button" style="text-align: center;"><?php echo $initial ?></h5>
</div>
</div>
</div>
<?php }
} ?>
ajax bootstrap-4 bootstrap-modal
ajax bootstrap-4 bootstrap-modal
asked Jan 2 at 0:34
user2843365user2843365
90213
90213
1
You are repeating your modal code inside the while loop, if you want the modal to refresh with new data you only need 1 modal box, not 1 for every user (although this is possible). The problem comes down to not having unique IDs
– Second2None
Jan 2 at 0:54
1
You don't need the php variables for the modal yet, you need to make an AJAX call to a PHP file that returns those variables for you to fill in the modal. Your modal should be outside the while loop. You also need to make sure when you are creating your html, all elements have a unique ID, if you take your modal out of the while loop you still have repeating IDs which will cause errors later on for you - <div id="photos", <img id="staff" etc.
– Second2None
Jan 2 at 1:10
1
Ideally you should have an ID column in your DB that allows you to reference each specific row and can be used to set IDs for html elements that reference that particular row.
– Second2None
Jan 2 at 1:10
1
It's normally easier to create a new PHP file for ajax calls and to return your results in JSON. Yes you would write your SQL query for retrieving users info in that file. All you would need to send is the ID and retrieve that specific row and the required columns to populate the modal fields.
– Second2None
Jan 2 at 1:21
1
You can use POST or GET that is up to you but generally speaking you should use POST.
– Second2None
Jan 2 at 1:22
|
show 3 more comments
1
You are repeating your modal code inside the while loop, if you want the modal to refresh with new data you only need 1 modal box, not 1 for every user (although this is possible). The problem comes down to not having unique IDs
– Second2None
Jan 2 at 0:54
1
You don't need the php variables for the modal yet, you need to make an AJAX call to a PHP file that returns those variables for you to fill in the modal. Your modal should be outside the while loop. You also need to make sure when you are creating your html, all elements have a unique ID, if you take your modal out of the while loop you still have repeating IDs which will cause errors later on for you - <div id="photos", <img id="staff" etc.
– Second2None
Jan 2 at 1:10
1
Ideally you should have an ID column in your DB that allows you to reference each specific row and can be used to set IDs for html elements that reference that particular row.
– Second2None
Jan 2 at 1:10
1
It's normally easier to create a new PHP file for ajax calls and to return your results in JSON. Yes you would write your SQL query for retrieving users info in that file. All you would need to send is the ID and retrieve that specific row and the required columns to populate the modal fields.
– Second2None
Jan 2 at 1:21
1
You can use POST or GET that is up to you but generally speaking you should use POST.
– Second2None
Jan 2 at 1:22
1
1
You are repeating your modal code inside the while loop, if you want the modal to refresh with new data you only need 1 modal box, not 1 for every user (although this is possible). The problem comes down to not having unique IDs
– Second2None
Jan 2 at 0:54
You are repeating your modal code inside the while loop, if you want the modal to refresh with new data you only need 1 modal box, not 1 for every user (although this is possible). The problem comes down to not having unique IDs
– Second2None
Jan 2 at 0:54
1
1
You don't need the php variables for the modal yet, you need to make an AJAX call to a PHP file that returns those variables for you to fill in the modal. Your modal should be outside the while loop. You also need to make sure when you are creating your html, all elements have a unique ID, if you take your modal out of the while loop you still have repeating IDs which will cause errors later on for you - <div id="photos", <img id="staff" etc.
– Second2None
Jan 2 at 1:10
You don't need the php variables for the modal yet, you need to make an AJAX call to a PHP file that returns those variables for you to fill in the modal. Your modal should be outside the while loop. You also need to make sure when you are creating your html, all elements have a unique ID, if you take your modal out of the while loop you still have repeating IDs which will cause errors later on for you - <div id="photos", <img id="staff" etc.
– Second2None
Jan 2 at 1:10
1
1
Ideally you should have an ID column in your DB that allows you to reference each specific row and can be used to set IDs for html elements that reference that particular row.
– Second2None
Jan 2 at 1:10
Ideally you should have an ID column in your DB that allows you to reference each specific row and can be used to set IDs for html elements that reference that particular row.
– Second2None
Jan 2 at 1:10
1
1
It's normally easier to create a new PHP file for ajax calls and to return your results in JSON. Yes you would write your SQL query for retrieving users info in that file. All you would need to send is the ID and retrieve that specific row and the required columns to populate the modal fields.
– Second2None
Jan 2 at 1:21
It's normally easier to create a new PHP file for ajax calls and to return your results in JSON. Yes you would write your SQL query for retrieving users info in that file. All you would need to send is the ID and retrieve that specific row and the required columns to populate the modal fields.
– Second2None
Jan 2 at 1:21
1
1
You can use POST or GET that is up to you but generally speaking you should use POST.
– Second2None
Jan 2 at 1:22
You can use POST or GET that is up to you but generally speaking you should use POST.
– Second2None
Jan 2 at 1:22
|
show 3 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000050%2fload-database-into-bootstrap-modal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000050%2fload-database-into-bootstrap-modal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You are repeating your modal code inside the while loop, if you want the modal to refresh with new data you only need 1 modal box, not 1 for every user (although this is possible). The problem comes down to not having unique IDs
– Second2None
Jan 2 at 0:54
1
You don't need the php variables for the modal yet, you need to make an AJAX call to a PHP file that returns those variables for you to fill in the modal. Your modal should be outside the while loop. You also need to make sure when you are creating your html, all elements have a unique ID, if you take your modal out of the while loop you still have repeating IDs which will cause errors later on for you - <div id="photos", <img id="staff" etc.
– Second2None
Jan 2 at 1:10
1
Ideally you should have an ID column in your DB that allows you to reference each specific row and can be used to set IDs for html elements that reference that particular row.
– Second2None
Jan 2 at 1:10
1
It's normally easier to create a new PHP file for ajax calls and to return your results in JSON. Yes you would write your SQL query for retrieving users info in that file. All you would need to send is the ID and retrieve that specific row and the required columns to populate the modal fields.
– Second2None
Jan 2 at 1:21
1
You can use POST or GET that is up to you but generally speaking you should use POST.
– Second2None
Jan 2 at 1:22