Twitter Bootstrap Modal Dialog not working well. - php

Good day, I am displaying members of a team each on a DIV. For each DIV, there is a message button for a user to send message. The message button fires up a Bootstrap modal dialog box where one can type the message. Now, my problem is that I want the name of person the message is being sent to, to appear at the top of the dialog box but it is not working. It only shows the name of the first person in the database even when I click on the third person.
<?php
require_once "include/db_handle.php";
$sql = "SELECT i.*, m.* FROM addclique i JOIN members m ON m.id = i.clique_id WHERE adder_id = :id";
foreach ($db->query($sql, array('id' => $_SESSION['id'])) AS $result)
{
echo "
<div class='user_container'>
Name: </span> {$result['surname']} {$result['firstname']}</br>
<a href='test.php' class='' data-toggle='modal' data-target='#basicModal'>Send Message</a>
</div>
<div class='modal fade' id='basicModal' tabindex='-1' role='dialog' aria-labelledby='basicModal' aria-hidden='true'>
<div class='modal-dialog'>
<div class='modal-content'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button>
<h4 class='modal-title points' id='myModalLabel'><span style ='color:black;'>To:</span> {$result['firstname']} </h4>
</div>
<div class='modal-body'>
<form action='send.php' method='POST'>
<textarea name='message' rows='10' cols='65'></textarea></br>
</form>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Cancel</button>
<button type='button' class='btn btn-primary' data-dismiss='modal'>Send Message</button>
</div>
</div>
</div>
</div>
</div>";
}
?>

<a href='test.php' class='' data-toggle='modal' data-target='#**basicModal**'>Send Message</a>
<div class='modal fade' id='**basicModal**' tabindex='-1' role='dialog' aria-labelledby='basicModal' aria-hidden='true'>
If you really want to do a modal for each user, consider do a variable for your modals.
Not sure, but something like that.
var $counter = 0;
foreach ($db->query($sql, array('id' => $_SESSION['id'])) AS $result)
{
echo "
<div class='user_container'>
Name: </span> {$result['surname']} {$result['firstname']}</br>
<a href='test.php' class='' data-toggle='modal' data-target='#basicModal".<?= $counter ?>." '>Send Message</a>
</div>
<div class='modal fade' id='basicModal".<?= $counter ?>."' tabindex='-1' role='dialog' aria-labelledby='basicModal' aria-hidden='true'>
<div class='modal-dialog'>
<div class='modal-content'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button>
<h4 class='modal-title points' id='myModalLabel'><span style ='color:black;'>To:</span> {$result['firstname']} </h4>
</div>
<div class='modal-body'>
<form action='send.php' method='POST'>
<textarea name='message' rows='10' cols='65'></textarea></br>
</form>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Cancel</button>
<button type='button' class='btn btn-primary' data-dismiss='modal'>Send Message</button>
</div>
</div>
</div>
</div>
</div>";
$counter++;
}
?>

You are creating modal for each user with same id i.e. basicModal. So if total user is 5 then 5 modal with same id is created on html page.
<a data-toggle='modal' data-target="#basicModal">Show Modal</a>
And in the above link you are calling #basicModal.So the js cant find the that particular user modal id because it is not unique.So it display the first modal with that id(or display none)
The problem should be solved if you use unique id in the link and modal .But a better option would be creating a single modal and setting the data through js so that multiple modal html is not placed in page.If you want to do it with jquery
Send Message
In jquery
$("#showModal").on("click",function(){
//set modal data
});

Related

i passing the session from php tags and then try to hide and show the div on page load based on if statement using jquery

I passing the user role from PHP $session['role'] tags and then try to hide and show the div on page load event based on if(!sessionRole=='admin') statement using jQuery.
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script type="text/ecmascript"> //script to hide show page on page load.
$(document).ready(function{
var sessionRole=<?php echo $session_ROLE ?>
if(!sessionRole=='admin'){
$('#admin_tool').hide();
}
});
</script>
<div class="col-md-4" >
<div class="widgets" style="margin-top: 15px;">
<div class='Recent'id='admin_tool' > // dive that i want to hide and show
<h4>Tools</h4>
<hr>
<div class='container'>
<div class='row'>
<div class='col'><button type='button' id="btn" class='btn btn-success btn-lg' style='min-width: 150px;'>Publish</button></div>
<div class='col'><button type='button' id="btn2" class='btn btn-warning btn-lg'style='min-width: 150px;' >Oppose</button></div>
</div>
<div class='row' style='margin-top: 10px;'>
<div class='col'><button type='button' class='btn btn-info btn-lg' style='min-width: 150px;'>Unpublish</button></div>
<div class='col'><button type='button' class='btn btn-danger btn-lg' style='min-width: 150px;'>Delete</button></div>
</div>
</div>
</div>
</div>
Don't!
If you want to securely hide something to a type of user, do it from the server.
Wrap the HTML lines for admin in a if condition.
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<div class="col-md-4" >
<div class="widgets" style="margin-top: 15px;">
<?php
if($session_ROLE == "admin"){
?>
<div class='Recent'id='admin_tool' > // dive that i want to hide and show
<h4>Tools</h4>
<hr>
<div class='container'>
<div class='row'>
<div class='col'><button type='button' id="btn" class='btn btn-success btn-lg' style='min-width: 150px;'>Publish</button></div>
<div class='col'><button type='button' id="btn2" class='btn btn-warning btn-lg'style='min-width: 150px;' >Oppose</button></div>
</div>
<div class='row' style='margin-top: 10px;'>
<div class='col'><button type='button' class='btn btn-info btn-lg' style='min-width: 150px;'>Unpublish</button></div>
<div class='col'><button type='button' class='btn btn-danger btn-lg' style='min-width: 150px;'>Delete</button></div>
</div>
</div>
</div>
<?php } // End if admin
?>
</div>

trying to send get value to modal

I'm trying to send the get variable from a anchor href to modal. but I cant seem to get the variable $_GET['edit2']. when I try to echo the variable it echo null which is obtain from the else statement. Thank you in advance
Part 1 : used to check if the get variable have been submitted or not.
if(isset($_GET['edit2'])){
$_SESSION['reply_id'] = $_GET['edit2'];
$reply_id = $_SESSION['reply_id'];
}
else{
$reply_id = "null";
}
Part 2 : the anchor href
<a href='?edit2=". $row2['reply_id'] ."' data-toggle='modal' data-target='#myModal'><small>edit</small></a>
part 3 : the modal form
<div class='modal fade' id='myModal' role='dialog'>
<div class='modal-dialog'>
<!-- Modal content-->
<div class='modal-content row'>
<div class='col-12'>
<div class='modal-header'>
<h5 class='modal-title'>Edit Reply</h5>
<button type='button' class='close' data-dismiss='modal'>×</button>
</div>
<div class='modal-body'>
<form id='r_edit_form' method='post'>
<div class='row'>
<label class='col-12'>Reply :</label>
</div>
<div class='row'>
<textarea class='col-12' form='r_edit_form' name='description' rows='4'>". $reply_id ."</textarea>
</div>
<div class='row'>
<input class='col-12 btn btn-dark' type='submit' name='edit_reply'>
</div>
</form>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
</div>
</div>
</div>
</div>

"If" Statement and Session

I have a form that is posting sessions back to my index page. Based on those sessions I am using echo to load a modal window from the results. I keep getting a 500 error. I can't figure out why.
if($_SESSION["submitemail"] == "fail"){
echo
"<script type='text/javascript'>
$(window).load(function(){
$('#betaalert').modal('show');
});
</script>
<div class='modal fade' role='dialog' id='formerror'>
<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'>Could not send message...</h4>
</div>
<div class='modal-body'>
<?
echo '<p>We are very sorry, but there were error(s) found with the form you submitted.</p>';
echo '<p>These errors appear below.</p><br /><br />';
echo $_SESSION['errormessage'].'<br /><br />';
echo '<p>Please go back and fix these errors.</p><br /><br />';
?>
</div>
<div class='modal-footer'>
<a href='#' class='btn btn-primary' data-dismiss='modal'>Close</a>
</div>
</div>
</div>
</div>";
}
else if($_SESSION["submitemail"] == "success"){
echo
"<script type='text/javascript'>
$(window).load(function(){
$('#betaalert').modal('show');
});
</script>
<!-- Modal -->
<div class='modal fade' role='dialog' id='formsent'>
<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'>Thank You <? echo $_POST['first_name']; ?> <? echo $_POST['last_name']; ?>!</h4>
</div>
<div class='modal-body'>
<?
echo '<p>We appreciate your business and will be contacting you as soon as possible. Please allow 48 hours for us to process your request before sending another. Thank you!/p>';
?>
</div>
<div class='modal-footer'>
<a href='#' class='btn btn-primary' data-dismiss='modal'>Close</a>
</div>
</div>
</div>
</div>";
}
else {
echo
"<script type='text/javascript'>
$(window).load(function(){
$('#betaalert').modal('show');
});
</script>
<!-- Modal -->
<div class='modal fade' role='dialog' id='betaalert'>
<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'>ANNOUNCEMENT!</h4>
</div>
<div class='modal-body'>
<p>However we are pleased to announce that our beta testing program is open to all ambulance companies interested. If you are interested use the contact form on the bottom of this page to send us a message with your contact information and we will get you started with testing ASAP!</p>
</div>
<div class='modal-footer'>
<a href='#' class='btn btn-primary' data-dismiss='modal'>Close</a>
</div>
</div>
</div>
</div>";
}
Nevermind, I figured it out! Thank you though, and for the future, I will make sure to post more information.

Is there a way to display at least 10 modal windows on single click?

I use this code to display modal whenever the user clicks the button.
<a href='#awesome' data-toggle='modal'><input type = 'button' class = 'btn btn-primary' value = 'Click Here' > </a>
.... </html>
<div class='modal fade' id='awesome' role='dialog'>
<div class='modal-dialog' style='width: 600px;'>
<div class='modal-content' style=''>
<div class='modal-header'>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class='modal-body' style = 'height: 500px; background-image:url(bground.jpg); background-repeat: no-repeat; ' >
</div>
<div class='modal-footer'>
<!--<a class="btn btn-default" data-dismiss="modal">Close</a>-->
</div>
</div>
</div>
Is there a way or function to display at least 10 modals with different positions on screen in just one click?
Yes, you can do that using Jquery.
Assuming the button has an id of 'modalOpen'
You can write:
$('#modalOpen').click(function(){
$('.modal1').show(function(){
$(this).css('top', '100px');
});
$('.modal2').show(function(){
$(this).css('top', '200px');
});
....
});
This way you can add as many modals you want which will open when the user clicks on the button with ID 'modalOpen'

I am having problems displaying a result from an array in a while loop

I am new to php and am having a problem echoing a result from the database. Everything above the div tags executes fine and displays in my table properly. But the problem I am having is within the div tags.
while($row = mysql_fetch_array($result))
{
if ($row['status']==0){
$row['status']="Inactive";
$lablestatus="label";
}
elseif ($row['status']==1){
$row['status']="Pending";
$lablestatus="label label-warning";
}
elseif ($row['status']==2){
$row['status']="Banned";
$lablestatus="label label-important";
}
elseif ($row['status']==3){
$row['status']="Active";
$lablestatus="label label-success";
}
echo "<tr>
<td>{$row['id']}</td>
<td class='center'>{$row['username']}</td>
<td class='center'>17</td>
<td class='center'>36</td>
<td class='center'>17</td>
<td class='center'>$458.66</td>
<td class='center'>Yes</td>
<td class='center'>{$row['register_date']}</td>
<td class='center'>2013-02-13 24:06:13</td>
<td class='center'>Yes</td>
<td class='center'><span class='$lablestatus'>{$row['status']}</span></td>
<td class='center'>
<a class='btn btn-success' href='view_user.php?id={$row['id']}'>
<i class='icon-zoom-in icon-white'></i>View</a>
<a class='btn btn-info' href='edit_user.php?id={$row['id']}'>
<i class='icon-edit icon-white'></i>Edit</a>
<a class='btn btn-danger btn-setting'>
<i class='icon-trash icon-white'></i>Delete</a>
</td>
</tr>
Here is where the problem occurs. When I click delete for a certain user, a popup is displayed. It asks "Are you sure you want to delete the user exampleuser?" The same username is displayed for each user in my table. So if i click delete for exampleuser2, It will ask if I want to delete exampleuser. Any idea how to fix this?
<div class='modal hide fade' id='myModal'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h3>Delete member</h3>
</div>
<div class='modal-body'>
<p>Are you sure you want to delete the user {$row['username']}?</p>
</div>
<div class='modal-footer'>
<a href='#' class='btn' data-dismiss='modal'>No</a>
<a href='delete_user.php?id={$row['id']}' class='btn btn-primary'>Yes</a>
</div>
</div>";
}
AS your pop up code is inisde while loop
<div class='modal hide fade' id='myModal'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h3>Delete member</h3>
</div>
<div class='modal-body'>
<p>Are you sure you want to delete the user {$row['username']}?</p>
</div>
<div class='modal-footer'>
<a href='#' class='btn' data-dismiss='modal'>No</a>
<a href='delete_user.php?id={$row['id']}' class='btn btn-primary'>Yes</a>
</div>
</div>";
Your div id is not unique id='myModal', I guess all the time it takes for the first/last div. There can not be multiple id a page
Make it dynamic <div class='modal hide fade' id='myModal_{$row['id']}'> some thing like this and change your js pop up code and try (I guess your are specifying this id while clicking the delete link there also make it dynamic to match proper pop ups).

Categories