Dropdown button stops working when navigate in it using ajax - php

Hello StackOverFlow Members,
I've used the bootstrap 5 dropdown button to get the tabs from the backend using ajax and everything is working, until I got to one of these tabs and click on a button, the dropdown stills exists and the data we got using ajax still exists, also it makes a new request so we made sure the data still exists.
but when we click on that dropdown, it doesn't longer open.
code used.
<li class="dropdownListMenu dropdown">
<span aria-expanded="false" data-bs-toggle="dropdown" aria-haspopup="true"
class="active color-white pointer" data-bs-toggle="tab"
onclick='<?php echo "get__data(" . $id . ")"; ?>'>
<i class="fa fa-folder tx-29"></i>
<?php echo $value['name']; ?>
<i class="fas fa-caret-down ms-1"></i>
</span>
<div class = "dropdown-menu tx-13" id = "get_data_dropdown_<?php echo $_id; ?>">
</div>
</li>
we used the javascript function to call the backend using ajax.
function get_data(id) {
$.post("ajax/get_data.php", {id: id,}, function (data) {
$("#results").html(data);
});
}
the returned data from the ajax call is a result of foreach with a link.
<a class="dropdown-item" onclick="get_classification(
<?php echo $tbl_level_id; ?>,
<?php echo $tbl_org_id; ?>,
<?php echo $tbl_classin_id; ?>,
<i class="fa fa-info-circle"></i>
<?php echo $value['name']; ?>
</a>
when we go to a link of these data returned I put them in an empty div
<div id="results"> </div>
so when we get the data we put it there.
now these data contain another link, when I click on any of these links/buttons, The dropdown stills exists and the data in it still exists, but it is no longer working.
I hope I explained It well.

I actually found an answer.
What happened is that I was adding a function to handle the problem before as it solved the issue, but after time it didn't longer work, I removed that function since I didn't need it.
<script>
$(function () {
//This function fixes the issue dropdown button doesn't open in a new page..
$('li>[data-bs-toggle="dropdown"]').each(function () {
$(this).click(function () {
if (!$(this).hasClass("show") && $(this).attr("aria-expanded") === "false") {
$(this).next().addClass("show");
$(this).attr("aria-expanded", "true");
$(this).addClass("show");
} else {
$(this).next().removeClass("show");
$(this).attr("aria-expanded", "false");
$(this).removeClass("show");
}
})
})
}
)
</script>

Related

How to perform a query when a button is clicked in this legacy php web app?

I am totaly new in PHP and I have the following problem implementing a change request on a very old legacy web application (so I can't change the used logic).
I have this php file used to show a popup:
<?php
require_once("../../../common/lib.php");
require_once("../../../common/define.php");
if(isset($_POST['id']) && isset($_SESSION['user']['id'])){
$id_booking = (int)$_POST['id'];
if(is_numeric($id_booking)){ ?>
<script>
function printElem(elem){
var popup = window.open('', 'print', 'height=800,width=600');
popup.document.write('<html><head><title>'+document.title+'</title><link rel="stylesheet" href="<?php echo getFromTemplate("css/print.css"); ?>"/></head><body>'+document.getElementById(elem).innerHTML+'</body></html>');
setInterval(function(){
popup.document.close();
popup.focus();
popup.print();
popup.close();
}, 100);
return true;
}
</script>
<div class="white-popup-block" id="popup-booking-<?php echo $id_booking; ?>">
<h2><?php echo $texts['CANCEL_BOOKING'] ?></h2>
<p>Sei veramente sicuro di voler cancellare la prenotazione?</p>
<?php echo $id_booking; ?>
<a href="#" class="btn btn-primary sendAjaxForm" data-action="/perform_query">
<i class="fa fa-power-off"></i>
Conferma
</a>
<?php
$result_remove_booking = $db->query("UPDATE pm_booking SET status= ".$id_booking);
?>
</div>
<?php
}
?>
<?php
}
?>
As you can see it contains a confirmation "button", this one:
<a href="#" class="btn btn-primary sendAjaxForm" data-action="/perform_query">
<i class="fa fa-power-off"></i>
Conferma
</a>
I want implement the feature that when this button is clicked the following defined query is perfromed, this one:
<?php
$result_remove_booking = $db->query("UPDATE pm_booking SET status= ".$id_booking);
?>
After that this operation is executed the popup have to be closed.
How can I correctly perform my query when this button is clicked?

Opening a PHP link in a jQuery UI dialog

$(function() {
$("#register").dialog({
autoOpen:false,
show: {
effect:"",
duration: 1000
},
hide: {
effect:"",
duration: 1000
}
});
$("#register-opener").click(function() {
$("#register").dialog("open");
});
});
<td><a id="register-opener" href="?id=<? echo "$members_row[id]"; ?>"><i class="glyphicon glyphicon-pencil" title="Edit"></i></a></td>
So what I'm trying to accomplish is to click on a link to be able to edit a certain users information, the problem is I can only get the popup to occur when replacing the href with href="#". Can someone assist. Thanks.
You need to make some changes in jQuery to achieve this.
$("#register-opener").click(function(e) {
e.preventDefault();
$("#register").dialog("open");
});
Above script will make sure that when ever user clicks on link it doesn't go to URL mentioned in href and execute following codes.
https://api.jquery.com/event.preventdefault/
If e.preventDefault() is called then default action of the
event(click in above case) will not be triggered.
There seems to be problem with dialogue function you have. Use prompt instead and be sure to remove icon inside a tag
jQuery("#register-opener").click(function() {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.getElementById("#yourid").innerHTML =
"Hello " + person + "! How are you today?";
//Or do your thing
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<a id="register-opener" href="?id=<?php echo '$members_row[id]'; ?>">
icon<i class="glyphicon glyphicon-pencil" title="Edit">
</i>
</a>
</div>
This might be better accomplished by putting your ID in a data- attribute rather than using href, which is meant to be used for specifying a URL for the web browser to follow.
<td><a id="register-opener" href="#" data-id="<? echo $members_row[id]; ?>"><i class="glyphicon glyphicon-pencil" title="Edit"></i></a></td>
You can then access the value of the data-id attribute in jQuery by using
$("register-opener").attr("data-id");

Ajax how to disable div or item

I am new to ajax, is there a way you can help me, how to disable all items once click is successful:
Here is my code for ajax:
if (!parent.hasClass('.disabled')) {
// vote up action
if (action == 'click') {
alert("test");
};
//how do i add disabled function on a particular div
// add disabled class with .item
parent.addClass('.disabled');
};
here is my index:
<?php while($row = mysql_fetch_array($query)): ?>
<div class="item" data-postid="<?php echo $row['recipe_id'] ?>" data-score="<?php echo $row['vote'] ?>">
<div class="vote-span"><!-- voting-->
<div class="vote" data-action="up" title="Vote up">
<i class="icon-chevron-up"></i>
</div><!--vote up-->
<div class="vote-score"><?php echo $row['vote'] ?></div>
</div>
<div class="post"><!-- post data -->
<p><?php echo $row['recipe_title'] ?></p>
</div>
</div><!--item-->
i jst want to disable the loop icon-chevron-up class.not just one but all.
Actually no ajax call needed here. I will only be done by using jquery. See the code
$(document).ready(function(){
$('.item').click(function(){
if (!parent.hasClass('.disabled')) {
parent.addClass('.disabled');
}
});
});
Here you have not mentioned, on what click you need the action, so i consider that on the div contains class='item', the action will be performed. I hope it will help.

php mysql + ajax + jquery + delete action

i am creating a comment system using the php and mysql to store data and ajax with jquery all the system work well but when it comes to the delete action nothing it happen like this button do not have any action or relation with the system can anyone help me ???
comment_box.php
<li class="comment-holder" id="_<?php echo $comment->comment_id; ?>">
<div class="user-img">
<img src="<?php echo $user->profile_img; ?>" class="user-img-pic" />
</div>
<div class="comment-body">
<h3 class="username-field">
<?php echo $user->userName; ?>
</h3>
<div class="comment-text">
<?php echo $comment->comment; ?>
</div>
</div>
<div class="comment-buttons-holder">
<ul>
<li id="<?php echo $comment->comment_id; ?>"class="delete-btn">X</li>
</ul>
</div>
</li>
comment_delete.js(i am testing the delete button with firebug)
$(document).ready(function() {
$('.delete-btn').each(function() {
var btn = this;
$(btn).click(function(){
console.log("the id " + btn.id);
})
});
});
Try this
$(document).ready(function() {
$('.delete-btn').each(function(i,el) {
$(el).click(function(){
console.log("the id " + $(this).attr('id'));
})
});
});
<li id="<?php echo $comment->comment_id; ?>"class="delete-btn">X</li>
Since you are missing whitespace here between the id value and the class attribute, I think the browser just does not assign that class to the element.
Edit (for people who don’t know what “missing” means):
Your PHP code will generate HTML output of the form
<li id="123"class="delete-btn">X</li>
whereas it should of course be
<li id="123" class="delete-btn">X</li>
Please always validate your HTML code before asking questions.
Try
$(document).ready(function() {
$('.delete-btn').click(function() {
var comment_id = $(this).attr('id');
alert(comment_id);
});
});

Multiple instances of SlideToggle which toggle everything

I have the following script which appears multiple times on my page. I have a simple slide toggle attached to <div class="info"> and contolled by <a rel="viewinfo"> tag, but when I click on the anchor, all the divs with class info slide.
here is the HTML/PHP
<div class="couplistMeta">
<a class='view' rel="viewinfo" href="#">View Coupon</a>
<a class="print" href="#">Print</a>
</div>
<div class="info">
<p><?php echo $rec2[4]." -- Phone: ".$rec2['phone']; ?></p><p><?php echo $rec2['address']." -- ".$rec2['city'].", ".$rec2['state']." ".$rec2['zip']; ?></p>
</div>
Here is the Javascript
$(document).ready(function() {
$('div.info').hide();
$("a[rel='viewinfo']").click(function() {
$('div.info').slideToggle(400);
return false;
});
});
I tried using $(this).next('div.info').slideToggle(400); but this just broke the effect. So $('div.info') is too general, how can I be more specific with the JS?
Try this:
$(document).ready(function() {
$('div.info').hide();
$("a[rel='viewinfo']").click(function() {
$(this).parent().next('.info').slideToggle(400);
return false;
});
});
The issue being due to $('div.info') implicitly iterating over all div elements with the class info on the page.

Categories