I am using Select2 for my dropdown option. I am populating it in a table form with MySQL data and the form is inside a bootstrap Modal, together with the button to edit or delete.
<div class="btn-group">
<a class="btn btn-primary" href="#"><i class="fa fa-home"></i> Area</a>
<a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#">
<span class="fa fa-caret-down"></span></a>
<ul class="dropdown-menu">
<?php
echo "<li><a class=\"open-EditRow\" data-areano=\"".$areano."\" data-area=\"".$area."\" data-centerno=\"".$centerno."\" title=\"Edit this row\" \"><i class=\"fa fa-pencil\"></i> Edit</a></li>";
echo "<li><a class=\"open-DeleteRow\" data-areano=\"".$areano."\" data-area=\"".$area."\" data-centerno=\"".$centerno."\" title=\"Delete this row\" \"><i class=\"fa fa-trash\"></i> Delete</a></li>";
?>
</ul>
</div>
Then the Edit button will open a Modal to edit details.
<div class="form-group">
<label class="col-sm-3 control-label">Center</label>
<div class="col-sm-5">
<?php
$e = mysql_query("select * from center") or die(mysql_error());
?>
<select class="populate placeholder" name="center" id="center">
<option value="">-- Select a center --</option>
<?php
while($f=mysql_fetch_array($e))
{
?>
<option value="<?php echo $f['center_no']; ?>" <?php if ($f['center_no']==$centerno) { ?>selected<?php } ?>><?php echo $f['center']; ?></option>
<?php
}
?>
</select>
</div>
</div>
This is how I pass the data to the Modal:
$('.open-EditRow').click(function(){
var areano = $(this).attr('data-areano');
var area = $(this).attr('data-area');
var centerno = $(this).attr('data-centerno');
$('#myModal #areano').val(areano);
$('#myModal #area').val(area);
$('#myModal #center').val(centerno);
$('#myModal').modal('show');
});
And the Select2 code:
function DemoSelect2(){
$('#center').select2();
}
But Select2 is displaying incorrect selected value. It is displaying the first item generated by the query, instead of the selected value.
But upon clicking the Select2 dropdown, the selected item is highlighted correctly.
What seems to be the problem here? Any guidance will be appreciated.
The workaround I did for this, and it somehow worked is to first remove the if statement which was used to compare the data. So my option just looked like this:
<option value="<?php echo $f['center_no']; ?>"><?php echo $f['center']; ?></option>
And then I declared the selected value on the function DemoSelect2()
function DemoSelect2(){
var centerno = $(this).attr('data-centerno');
$('#center').select2("val", centerno);
}
Now, my Select2 dropdown looked like this:
Upon clicking the dropdown, it highlighted the correct selected value:
I don't know if this is the correct fix for this but, this worked for me.
Related
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 } ?>
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.
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
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(...)
In this page i have done table listing and each tr have two link are :-
1) Adult and 2) Child
When Click on particular link(for Adult or child) show the div at that position and all other position's div are hide. if next or any other link is click then hide previous link div.
I want to show the hidden div(as i given the hidden properties in div) at when click on particular link
------------Here are the HTML :: ------------
<td>
<div class="tab-inner">
<p><span><strong>Space : <?php echo $row->space; ?></strong></span>
</p>
<p>Adult $<?php echo $row->adultPrice; ?>
<!-- <div id="SpanAdultAllot"></div>-->
</p>
<div style="display:none;" class="adult-wrapper">
<div class="adult-box">
<div class="adultborder">
<div id="adultallot" class="adult-msg">Adult Allocation</div> <span class="adult-select-no"></span>
</div><span class="adultarrow"></span>
<label id="adult" class="adultselect">Adult :</label>
<select class="selectorbox" id="adultselect" name="adult">
<option value="">--</option>
<?php for($i=1; $i<=$row->space; $i++) { ?>
<option value="<?php if($i==$row->space) echo " selected='selected' "; ?>">
<?php echo $i;?>
</option>
<?php } ?>
</select> <strong></strong>
</div>
</div>
<?php if($row->childPrice
<>'0'){ ?>
<p>Child $<?php echo $row->childPrice; ?>
</p>
<!--<div id="SpanChildAllot"></div>-->
<?php } ?>
</div>
</td>
----------Here JS code::-----------
$('a[name="allocation"]').live("click", function () {
$(this).parent().next('.adult-wrapper').show();
$('.adult-select-no').live("click", function () {
$(this).closest('.tab-inner').find('.adult-wrapper').hide();
});
});
Add / include jquery-1.9.0.min.js file.
Create one div to show:
<div id="details_link" style="">
Show Link
</div>
Create another div to hide:
<div id="details_div" style="display:none;">Test show and hide div
Hide Link
</div>
Some tips for avoiding errors when using different browsers:
Don't write Javascript code in the href attribute, always put it in onclick.
To hide div, write in its style display:none or block.
Don't use visibility=hidden.