I'm trying to create a simple buddy list for a web application. I'm looping through an SQL table to retrieve the current user's buddy list. Each row in the table has a button to display a modal of the selected user's ID. The current issue I'm having is that it's only showing the modal for the last user in the list, for everyone else it doesn't pop up. I need the modal to pop up for each user I click on and display that user's ID.
Example:
Here is the code I have so far:
HTML/PHP:
<?php
// Full SQL retrieval omitted above
$buddyUid = '';
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$buddyUid = $row['buddy_uid'];
echo "<tr>";
echo "<td> <input type='checkbox'></td>";
echo "<td>" . $buddyUid . "</td>";
echo "<td> <button type='button' onclick='javascript:selectBuddy($(this)); ' data-bs-toggle='modal' data-bs-target='#modal-" . $buddyUid . "'><i class='fas fa-share-square'></i></button></td>";
echo "</tr>";
}
?>
<!-- Modal -->
<div class="modal fade" id="modal-<?=$buddyUid?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<?= $buddyUid?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Javascript:
function selectBuddy(buddy) {
$('#modal-' + buddy).modal('show');
}
$(this) refers to the element in the DOM to which the onclick attribute belongs:
in your case to get only buddy_id just change onclick event
from this
...onclick='javascript:selectBuddy($(this));'...
to this
...onclick='javascript:selectBuddy(".$buddyUid.");'...
also change your Modal id from
<div class="modal fade" id="modal-<?=$buddyUid?>" ...
to
<div class="modal fade" id="modal-buddy" ....
then javascript code like this
function selectBuddy(buddy) {
$('#modal-buddy').find('.modal-body').html(buddy);
$('#modal-buddy').modal('show');
}
Related
I want to echo my results in the bootstrap modal class without the page reloading or refreshing but each time i click the submit button, it reloads and it escapes the modal class... I am using foreach loop and i guess each time the loop counts after the first time, it clears everything...
<?php
$count_main_odd = 0;
$main_odds_error = $main_odds = "";
if(isset($_POST["submit"])){
if(empty($_POST["total_odds"])){
echo $main_odds_error = "<div class='error-msg'>You have not selected any odds</div>";
}else{
$main_odds = $_POST["total_odds"];
$total = 1;
if(!empty($main_odds)){
echo "You have selected the following games ";
foreach ($main_odds as $final_odd){
echo $final_odd;
$total = $total * $final_odd;
$total1 = $final_odd . ", ";
}
}
$count_main_odd = count($main_odds);
echo number_format($total, 2);
}
}
// calculating selected games
?>
This is the submit button from the page
<input type="submit" name="submit" value="Calculate Selected odds" class="btn btn-outline-success btn-sm card-link" data-toggle="modal" data-target="#exampleModalOdds"/>
This is the modal class, i had to remove the code because it looks untidy
<!-- Modal -->
<div class="modal fade" id="exampleModalOdds" 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">Modal title</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>
How can i not make the page reload after clicking the submit button...
This is the input type
<input type="checkbox" name="total_odds[]" value="<?= $game->odds; ?>-<?= $game->home; ?>-<?= $game->away; ?>"/>
I created a table with too buttons: delete and another one to "see
other profiles" (this button displays a modal, and the button loops
throughout the table. It should display the modal, but it doesn't for
some reason. I already corrected as many mistakes as I could, yet the
button doesn't display the modal)
function crea_tabla_foros() {
/* here I create a query to select my tables*/
/*I execute it */
$rs = ejecutar($q);
/*I create a while to make the table dynamic */
$contenido = " ";
while($r = mysqli_fetch_array($rs)) {
/*calling mysql*/
$id_foro = $r['id_foro'];
$nombreforo = $r['nombreforo'];
$usuario = $r['usuario'];
$comentarios = $r['comentarios'];
$id_comentarios = $r['id_comentarios'];
$contenido .= "
<tr>
<td style='color:black;'>$nombreforo</td>
<td style='color:black;'>$usuario</td>
<td style='color:black;'>$comentarios </td>
<td>
<form method='post' action='bak_foros.php'>
<input type='hidden' name='id_comentarios' value='$id_comentarios'>
<input class='trash' type='submit' name='eliminar' value='Eliminar'>
</form>
/*HERE IS THE BUTTON FOR THE MODAL*/
<button data-toggle='modal' data-target='#ver'>VER</button>**
</td>
</tr>"
Here is the modal that should display and it is located inside the
body but outside the PHP function:
<div id="ver" 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">PERFILES DE PARTICIPANTES</h4>
</div>
<div class="modal-body">
<p></p>
</div>
<div class="modal-footer">
<button class="publicar">PUBLICAR</button>
<button type="button" class="btn btn-default" data-dismiss="modal">CANCELAR</button>
</div>
</div>
</div>
</div>
Here is the link click here
You have placed you modal inside the following div
<div class="filterDiv pendientes">
Which has a css display:none property. Place it outside this div and it should show.
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.
I have a button that is created using a while loop. so the while loop creates a table row for each row in a mysql table.
I have one line of code for the button which is created again for each row of the table and each button has a different value based on the id for that record.
$order .= '<td>Edit</td>';
The problem is that I want to use that $row['ID'] and view it in a modal so I can retrieve the record with the same ID using mysqli query.
Here is the code for the modal.
<div class="modal fade" id="myModal" 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">Edit Data</h4>
</div>
<div class="modal-body">
i want to save the id in a variable here so i can use it in a php script for this modal
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
put this inside your loop
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#message<?php echo $row['id'];?>">Message</button>
<div id="message<?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>Some text in the modal.</p>
<?php echo $row['id'];?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
If, I got you correct.
The modal trigger button with $row['ID']
$order .= '<td>Edit</td>';
Bootstrap Modal event to fetch the $row['ID'] value with Ajax method
$(document).ready(function(){
$('#myModal').on('show.bs.modal', function (e) {
var rowid = $(e.relatedTarget).data('id');
$.ajax({
type : 'post',
url : 'fetch_record.php', //Here you will fetch records
data : 'rowid='+ rowid, //Pass $id
success : function(data){
$('.fetched-data').html(data);//Show fetched data from database
}
});
});
});
Modal HTML
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Data</h4>
</div>
<div class="modal-body">
<div class="fetched-data"></div> //Here Will show the Data
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Last Create fetch_record.php, retrieve the record and show in modal
<?php
//Include database connection
if($_POST['rowid']) {
$id = $_POST['rowid']; //escape string
// Run the Query
// Fetch Records
// Echo the data you want to show in modal
}
?>
adding the modal in a loop is auto duplicating the html content when it is not really necessary! it is not a good practice to add a modal to each item in a loop instead call the data only when it is requested!
here is an example add the following button to a loop in php
<button data-id="<?php echo $note['id']; ?>" onclick="$('#dataid').text($(this).data('id')); $('#showmodal').modal('show');">Click me </button>
on your modal you can know use that id to make a request to your db or any json
example assuming you are using a modal called showmodal use this info to make a new request to your database or desire class or function!
<?php $dataid = '<span id="dataid"/>'; echo $dataid; ?>
The echo $dataid is only to show you that the id of the clicked item in the loop really works.
as you can see here we are only adding 1 single html template in the loop and we are also only calling data when it is being requested! this will save memory and also will be faster.
I hope this helps many people
In my view, I am querying and displaying all the rows from the database. All the rows are displayed fine except the problem comes in when i use a bootstrap modal (or bootstrap dialog box). I want to display the email for each query using modal (the dialog box) but the problem is that in each query the email in the first row of the table is the one that is being displayed for all the other queries...
what might i have done wrong here. thnx
my View
<?php
foreach($query as $row)
{ ?>
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Title of the data from the database
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">User email for this user</h4>
</div>
<div class="modal-body">
<?php echo $row->email?><!--this is where my problem is-->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
You are using duplicated id="myModal" therefore your button always open the same modal.
You can try to create unique Modal IDs and buttons for each of your rows. You can use row ID or something similar to differentiate
For example:
<button ... data-target="#myModal-<?php echo $row->id;?>">
...
<div class="modal fade" id="myModal-<?php echo $row->id;?>">
...