sending user a mesage by passing their id to a modal box - php

I am trying to create a message form into a bootstrap modal by Passing the user username/id to the modal for identification.
here is an html/php code that made a list of registered users.
<div class="col-lg-4 col-md-4 col-sm-4 mb">
<div class="content" style="margin-bottom:5px;">
<ul class=" extended inbox">
<div class="notify-arrow notify-arrow-green"></div>
<li>
<p class="green">People you can follow</p>
</li>
<?php
//Show all users
foreach ($users as $row) {
?>
<li style="padding:3px; margin-bottom:3px; width:100%; background:#CF9;">
<a href="index.html#">
<span class="photo" style="float:left; padding:4px;"><img alt="avatar" src="uploads/<?php echo $row['pix']; ?>" width="50" height="50"></span>
<span class="subject" style="float:left;">
<span class="from">
<?php
echo ''.$row['user_lastname']. ' '.$row['user_firstname'].'';
?>
<a class="" data-toggle="modal" data-target="#profile_pic_modal"><span class="glyphicon glyphicon-comment"></span></a>
</span><br>
<span class="time">
<?php
echo (($row['receiver'] === $row['user_id'] && $row['sender'] === $user_id)
? '<button class="btn follow following " rel="'.$row['user_id'].'">Following</button>'
:' <button class="btn follow" rel="'.$row['user_id'].'">
<i class="fa fa-user-plus alert-info"></i> Follow </button>');
?>
</span>
</span>
</a>
</li><br>
<?php
}
?>
</ul>
</div>
When a user click on another user's message icon he/she should be able to send message. Could someone please show me how to do this using php/mysqli and ajax

First add a data-(anyName) tag like below
<?php $ID = 'The persons Username'; ?>
<a href='#' class='btn btn-warning btn-xs open-AddBookDialog' data-id='$ID' data-toggle='modal'>My Modal</a>
Then put a input tag inside your modal body with a ID of example(MyID)
<input type="text" id="MyID">
Then using the code below (jquery) this will pass the value of data-id into that input tag on modal show.
$(document).on("click", ".open-AddBookDialog", function () {
var myId = $(this).data('id');
$(".modal-body #bMyID").val(myId);
$('#myModal').modal('show');
});

Information like username (login?) and id of a logged user should be disposed by Session, unlike you have good reasons for not do that. When the user enters in the system, you will already load a bunch of data to be sure he is valid, so the practical way to not retrieving always all the data from the user logged in, that are currently using your system, it's always good to use SESSIONs to handle that (imagine a ton of users using your system and you needing to retrieve each of them to each click and routine inside the system).
In a practical way, just open your modal and validate your routine using the started session with the user information settled in.
To clarify about the OTHERS users data (those listed that the current user will click to send a message) you can have to methods:
YOUR HTML:
<a href="#" data-userid='<?= $row['user_id']; ?>'>
<span class="photo" style="float:left; padding:4px;"><img alt="avatar" src="uploads/<?php echo $row['pix']; ?>" width="50" height="50"></span>
<span class="subject" style="float:left;">
<span class="from">
<?php
echo ''.$row['user_lastname']. ' '.$row['user_firstname'].'';
?>
<a class="" data-toggle="modal" data-target="#profile_pic_modal"><span class="glyphicon glyphicon-comment"></span></a>
</span><br>
<span class="time">
<?php
echo (($row['receiver'] === $row['user_id'] && $row['sender'] === $user_id)
? '<button class="btn follow following " rel="'.$row['user_id'].'">Following</button>'
:' <button class="btn follow" rel="'.$row['user_id'].'">
<i class="fa fa-user-plus alert-info"></i> Follow </button>');
?>
</span>
</span>
</a>
#Modal (Assuming you're using v4)
<div class="modal hide fade" id="user-message">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3 class="hdrtitle">Hey <?= $_SESSION['login']; ?>, Send message to <span></span></h3>
</div>
<div class="modal-body">
<label for='msg'>Message:</label>
<textarea id='msg' rows="4" cols="50" name='msg'></textarea>
<button id='send-msg'>Send Msg</button>
</div>
</div>
and then showing it with:
$('a').on('click', function(){
//retrieve all other user data
$.get( "yourcode.php?userid=" + $(this).data('userid'), function( data ) {
$('.modal h3 span').html(data.username);
//FILL YOUR MODAL AS YOU WISH
$('.modal').modal('toggle');
});
});
But the best way is to click and mount the modal in a partial view bringing the HTML already mounted with all information you want just to trigger the modal routine, avoiding html structure that the user don't really needs at first time access the page.
$('a').on('click', function(){
//retrieve all other user data
$.get( "partialmodal-sendmsg.php?userid=" + $(this).data('userid'), function( data ) {
$('body').append(data);
//FILL YOUR MODAL AS YOU WISH
$('.modal').modal('toggle');
});
});
Some must reads about:
When should I use session variables instead of cookies?
http://cse.unl.edu/~riedesel/pub/cse413/Project%202/SESSION.pdf

Related

How to make the button visible and hidden according to requirement

I want to display the button only when $safe_cus is 1.But the button gets displayed when the browser is resized.
<?php if($safe_cus == 1) {?>
<div class="col-sm-6 hidden-xs">
<div class="pull-right">
<a class="btn btn-primary" href="<?php echo $action_multiple; ?>"><?php echo $button_multiple_bill_payment; ?></a>
</div>
</div>;
<?php } ?>
one more thing you can do is write CSS based on your condition for ex
if($safe==1){.css{display:none;}}else{.css{display:blockl}}
Now put CSS as a class on your button
hope this helps you.
u can use javascript:
first set an id for your div;
for example "parentdiv"
then get your div to you javascript code:
document.getElementById("parentdiv");
and set an onload function for ur body tag:
body onload="show()"
then you can do ur display stuff with php:
<?php if($safe_cus == 1) {?>
<div class="col-sm-6 hidden-xs">
<div class="pull-right">
<a class="btn btn-primary" href="<?php echo $action_multiple; ?>"><?
php echo $button_multiple_bill_payment; ?></a>
</div>
</div>;
<script>
function show(){
var div = document.getElementById("parentdiv");
div.style.display="block";
}
</script>
<?php } ?>

Reduce Ajax implementation time / latency?

I hava a page having multiple picture in grid view loaded though a php loop as follows
<?php if($rows){
$i = 0;
while($resImage = mysqli_fetch_array($fetchImg)){?>
<div class="col-md-4">
<div class="user-box">
<h6 class="view-gallery-btn"><?php echo $resImage['image1']; ?></h6>
<?php $imgPath = base64_decode($_REQUEST['gallyId'])."_".$_SESSION['unique_id']; ?>
<i class="fa fa-cloud-download" aria-hidden="true"></i>
<img src="poweradmin/Gallery/<?php echo $imgPath?>/<?php echo $resImage['image1']; ?>" alt="" class="img-fluid">
<ul class="new-gallery-btn add-listing-gallery">
<li>
<i class="fa fa-star" aria-hidden="true"></i>
</li>
<li>
<i class="fa fa-trash-o" aria-hidden="true"></i>
</li>
<li>
<input name='ids[]' type='checkbox' id='ids' class="form-check-input custom-check-box checkboxes" value="<?php echo $resImage['id']; ?>">
</li>
</ul>
</div>
</div>
<?php } } ?> `
each image has a star icon that invokes an Ajax code as follows
$("a.redFav").click(function(){
var current = this ;
var status_id = $(this).attr('href').split('=');
var arr = status_id[1];
$.ajax({
type:'post',
url:'addUserPics.php',
data:{arr: arr},
success:function(msg){
console.log("User Like Applied");
$(current).toggleClass("user-fav");
}
});
return false;
});
this calls a php file that updates the corresponding field in the database but the issue is that if there are many images in the page ( ie around 100+ ) and some of them are selected (ie the icon calling Ajax is clicked ) eg 10 only few of them are updated. Each time the success stats are different it seems that its due to Ajax call delay or incomplete calls.
Kindly suggest a way to handle the same.

How can I send Passport number in url through button?

I Just want to send passport number in the url on the next page. I know it will go into the url like this continue-to-pay.php?pno=$passportnumber".
But how will I post passport number on the next page.
<?php
include'connection.php';
$pno=$_GET['pno'];
$sql="SELECT * FROM user WHERE passportnumber='$pno'";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query))
{
?>
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-5 text-right" id="review">
<ul>
<li class="list">Passport Number </li>
</ul>
</div>
<div id="review2">
<ul>
<li class="list"><?php echo $row['passportnumber']; ?></li>
</ul>
</div>
<br><br>
<div class="col-xs-6 col-sm-12 col-md-12 col-lg-12" align="center"
valign="top">
<a href="e-visa-application.php?actionType=edit" class="btn btn-primary
btn-lg mgn-btm mgn-top ">Edit</a>
<a href="process.php" class="btn btn-primary btn-lg mgn-btm mgn-top
">Continue to pay</a>
</div>
if you got the PNO at your URL you can get it in a PHP file using $_GET :
<?php
if (isset($_GET['pno'])) {
echo $_GET['pno'];
}else{
// Fallback behaviour goes here
}
?>
You are probably searching for "The PHP session system" to keep the pno in the session while the user is going through the steps.
The PHP session system lets you store securely data in the $_SESSION global array. A example is to store the pno in the session.
session_start();
$_SESSION['pno'] = $pno;
Then, you can access that information on all other pages:
if (isset($_SESSION['pno']))
echo $_SESSION['pno'];
More info about sessions here
You can use $_REQUEST:
<?php
$_REQUEST['pno'] = $pno;
?>
you can use it to retrieve the pno in the continue-to-pay.php?pno=$passportnumber

jQuery .bind only working on first element

I am trying to create a jQuery function that will allow me to add, edit, and delete articles on my site. Right now, I am trying to write the delete functionality. Basically, when the delete button is clicked, it will send the id of that specific article to a php script via ajax request. It gets the article ID from the ID attribute of the article element.
My problem is, that when the Delete button is clicked I get an alert box with the article ID on the first article, but not on the rest.
Here is my code:
HTML/PHP:
<div id="contentLeft">
<?php
foreach($data->blogPosts as $post){
?>
<article id="<?php print $post->ID; ?>" class="article">
<div id="options">
<div id="edit"><a class="btn btn-danger" href="#" id="editButton"><i class="fa fa-pencil"> Edit</i></a></div>
<div id="delete"><a class="btn btn-danger" href="#" id="deleteButton"><i class="fa fa-trash-o">Delete</a></i></div>
</div>
<div id="articleTitle"><?php echo $post->Title; ?></div>
<div id="content">
<?php echo $post->Content; ?>
</div>
<div id="date"><?php echo date('F jS, Y', strtotime($post->Date)); ?></div>
</article>
<?php
}
?>
</div>
My jQuery:
$("#deleteButton").bind('click', function(){
var id = $("article").closest("article").attr("id");
alert(id);
});
I appreciate the help!
In a given document ids should be unique; no more than one element should have the same ID. Please change your id's to classes and change your js accordingly:
HTML
<a class="btn btn-danger deleteButton" href="#"> ....
JS
$(".deleteButton").bind('click',...
You can add only one element with the same id.
Probably you should change id to class:
<div class="delete"><a class="btn btn-danger" href="#" class="deleteButton"><i class="fa fa-trash-o">Delete</a></i></div>
and in your js code:
$(".deleteButton").bind(...)

Giving Twitter Bootstrap Modals Dynamic IDs with PHP

I am trying to generate unique IDs for each modal, as I will be loading in an address specific map to each modal. My issue is that when I click the link:
<a data-toggle="modal" href="#myModal_<? echo $meeting['ID'] ?>" ><? echo $meeting['Address'].", ".$meeting['City'] ?></a>
it tries to take me to mypage.php/#myModal_8 (or whatever the given ID is, instead of pulling up that specific modal. The modals work great is I don't have the ID attached, but they won't load dynamic content that way.
<a data-toggle="modal" href="#myModal_<? echo $meeting['ID'] ?>" ><? echo $meeting['Address'].", ".$meeting['City'] ?></a>
</p>
<div class="modal hide" id="#myModal_<? echo $meeting['ID'] ?>">
<div class="modal-body">
<? echo $meeting['Address'] ?>
</div>
<div class="modal-footer">
Close
</div>
</div>
Any help on this would be much appreciated. Thank you!
It's all about the ID you are giving to the modal : id="#myModal_<? echo $meeting['ID'] ?>"
The # is only needed for the jQuery selector, not the ID. Which would give :
<div class="modal hide" id="myModal_<? echo $meeting['ID'] ?>">
Further more, you should open your php tags with <?php for the reasons described here : Are PHP short tags acceptable to use?

Categories