Queueing old click event values. - php

I have a problem and just can't find where the old values are coming from. What I have is a forum and list of replies where the user has the ability to rate good advice, bad advice on each reply. If you click on one it fires off a java script and the database is updated... the other button hides to show something happened. Where i am running into problems is on a fresh page refresh the first button works as designed but all others are passing not only the next click event values (which the script responds where you have already voted) but is passing ALL the previous clicked values. If I vote on four different posts then all four values post and three showing I have already voted. How can I purge the previous click event values?
<script>
function doGABAbad(forum_post_id,type){
$(".badadviceclick").click(function(){
var element = $(this);
var I = element.attr("id");
var info = 'id=' + I;
$("#gaba_float_right").html('<img src="loader.gif"/>').show();
$.post('updateGABA.php', {forum_post_id: forum_post_id,type: type}, function(data){
if(isNaN(parseFloat(data))){
alert(data);
}else{
$('#'+forum_post_id+'_'+type+'s').text(data);
}
$('#goodadvice'+I).fadeOut(200).show();
});
});
}
</script>
<script>
function doGABAgood(forum_post_id,type){
$(".badadviceclick").click(function(){
var element = $(this);
var I = element.attr("id");
var info = 'id=' + I;
$("#gaba_float_right").html('<img src="loader.gif"/>').show();
$.post('updateGABA.php', {forum_post_id: forum_post_id,type: type}, function(data){
if(isNaN(parseFloat(data))){
alert(data);
}else{
$('#'+forum_post_id+'_'+type+'s').text(data);
}
$('#badadvice'+I).fadeOut(200).show();
});
});
}
</script>
This is where I am building my Div's
<div id="goodadvice'.$forum_post_id.'">
<a id="'.$forum_post_id.'" class="badadviceclick" href="#" onClick="return false" onmousedown="javascript:doGABAgood('.$forum_post_id.',\'goodadvice\');">Good Advice '.$good_advice.'</a>
</div>
<div id="badadvice'.$forum_post_id.'">
<a id="'.$forum_post_id.'" class="badadviceclick" href="#" onClick="return false" onmousedown="javascript:doGABAbad('.$forum_post_id.',\'badadvice\');">Bad Advice '.$bad_advice.'</a>
</div>

The reason for that is you attach events in both javascript and jquery (jquery based event in javascript based event). you need to use one or another.
<script>
function doGABAbad(forum_post_id,type){
var element = $(this);
var I = element.attr("id");
var info = 'id=' + I;
$("#gaba_float_right").html('<img src="loader.gif"/>').show();
$.post('updateGABA.php', {forum_post_id: forum_post_id,type: type}, function(data){
if(isNaN(parseFloat(data))){
alert(data);
}else{
$('#'+forum_post_id+'_'+type+'s').text(data);
}
$('#goodadvice'+I).fadeOut(200).show();
});
}
function doGABAgood(forum_post_id,type){
var element = $(this);
var I = element.attr("id");
var info = 'id=' + I;
$("#gaba_float_right").html('<img src="loader.gif"/>').show();
$.post('updateGABA.php', {forum_post_id: forum_post_id,type: type}, function(data){
if(isNaN(parseFloat(data))){
alert(data);
}else{
$('#'+forum_post_id+'_'+type+'s').text(data);
}
$('#badadvice'+I).fadeOut(200).show();
});
}
</script>

First modify the HTML like so:
<div id="goodadvice'.$forum_post_id.'">
<a id="'.$forum_post_id.'" class="goodadviceclick" href="javascript:void(0);">Good Advice '.$good_advice.'</a>
</div>
<div id="badadvice'.$forum_post_id.'">
<a id="'.$forum_post_id.'" class="badadviceclick" href="javascript:void(0);">Bad Advice'.$bad_advice.'</a>
</div>
Next, you can refactor the functions like so:
function doGABA(e) {
e.preventDefault();
var element = $(this);
var forum_post_id = element.attr("id");
var type = element.hasClass('goodadviceclick') ? 'good' : 'bad';
var info = 'id=' + I;
$("#gaba_float_right").html('<img src="loader.gif"/>').show();
$.post('updateGABA.php', {forum_post_id: forum_post_id, type: type + 'advice'}, function(data){
if(isNaN(parseFloat(data))){
alert(data);
}else{
$('#'+forum_post_id+'_'+type+'s').text(data);
}
$('#' + type + 'advice'+I).fadeOut(200).show();
});
}
Then remove the onmousedown attributes on your anchor tags, and do it in jQuery:
$('.goodaviceclick, .badadviceclick').click(doGABA);

Related

send parameters to bootstrap modal

I have number of links that open bootstrap modal popup:
Edit 15
Edit 32
the problem - when the modal open it should get the link parameters:
data-act="edit" data-itinID="5" data-secID="15"
Well, it's not happening...
The script in the links page is:
<script>
// send data to modal file
$('#manageSec-model').on('show.bs.modal', function (e) {
var button = $(e.relatedTarget);
var act = button.data('act');
var itinID = button.data('itinid');
var secID = button.data('secid');
var date = button.data('date');
var $modal = $(this);
var info = 'act=' + act + "&itinID=" + itinID + "&secID=" + secID + "&date=" + date;
// alert(info);
$.ajax({
cache: false,
type: 'GET',
url: 'itinPage-secManage.view.php',
data: info,
success: function(data) {
$modal.find('.modal-body').html(data);
setTimeout(function(){ //added this line.
setImageUploader()
})
}
});
});
</script>
Modal:
echo "itinID: ".$_GET['itinID']." secID: ".$_GET['secID'];
Result in Modal:
itinID: secID:
itinID: 5 secID: 15
two lines (not sure why...)
for this to work you will have to call ajax on click of the anchor tag and for passing the values to ajax you can do following on click of anchor tag
$(this).attr("id");
or
$(this).attr("data-secID");//haven't tried this 2nd one
and then launch modal with JavaScript or jQuery with
$("#someId").modal();
Try changing
var button = $(e.relatedTarget);
to
var button = $(e.target);

How do i update a div tag after a ajax call?

I have script with multiple buttons which when clicked run a php script via AJAX. I would now like the result to show in the div with the button. I have tried this and parent but neither work.
Example below: when clicked on .showme I want the result to show in the #here div within the same its parent.
$(document).ready(function() {
$('.showme').bind('click', function() {
var id = $(this).attr("id");
var num = $(this).attr("class");
var poststr = "request=" + num + "&moreinfo=" + id;
$.ajax({
url: "../../assets/php/testme.php",
cache: 0,
data: poststr,
success: function(result) {
$(this).getElementById("here").innerHTML = result;
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='request_1 showme' id='rating_1'>More stuff 1
<div id="here"></div>
</div>
<div class='request_2 showme' id='rating_2'>More stuff 2
<div id="here"></div>
</div>
<div class='request_3 showme' id='rating_3'>More stuff 3
<div id="here"></div>
</div>
I think this will do:
$(document).ready(function () {
$('.showme').bind('click', function () {
//keep the element reference in a variable to use in ajax success
var _this = $(this);
var id = $(this).attr("id");
var num = $(this).attr("class");
var poststr = "request=" + num + "&moreinfo=" + id;
$.ajax({
url: "../../assets/php/testme.php",
cache: 0,
data: poststr,
success: function (result) {
//use the element reference you kept in variable before ajax call instead of $(this)
//also use jQuery style, getElementById is undefined if you call after $(this)
//.find() will call it's child in any level, also better you use classes instead of using same id multiple times
_this.find("#here").html(result);
}
});
});
});

How to make dynamically add or remove button

When I click add button, it is changed remove button. But I can not change remove button to add button again, When I click remove.
<head>
<script type="text/javascript" src="assets/js/jquery-2.0.3.min.js"> </script>
<script type="text/javascript" >
$(document).ready(function()
{
$(".add").click(function(){
var element = $(this);
var I = element.attr("id");
var info = 'id=' + I;
$("#" + I).attr("class", "remove");
$(".remove").text("Remove");
return false;
});
});
</script>
<script type="text/javascript" >
$(document).ready(function()
{
$(".remove").click(function(){
var element = this.id;
var I = element.attr("id");
var info = 'id=' + I;
$("#" + I).attr("class", "add");
$(".add").text("add");
return false;
});
});
</script>
<a class="add" id="1" href="#"> Add </a>
When you modify DOM elements with jQuery, anything that is loaded within $(document).ready() won't recognize the change. What you can do is wrap each step in a function, call these on document.ready, then call each again at the end of the other function:
<script type="text/javascript" >
$(document).ready(function()
{
addFunction();
removeFunction();
});
function addFunction(){
$(".add").click(function(){
var element = $(this);
var I = element.attr("id");
var info = 'id=' + I;
$("#result").text(I);
$("#" + I).attr("class", "remove");
$(".remove").text("Remove");
removeFunction();
return false;
});
}
function removeFunction(){
$(".remove").click(function(){
var element = $(this);
var I = element.attr("id");
var info = 'id=' + I;
$("#result").text(I);
$("#" + I).attr("class", "add");
$(".add").text("add");
addFunction();
return false;
});
}
</script>
You have both functions listed in document.ready, so you can't attach an event handler to the .remove class since it doesn't exist yet. I would suggest using delegated events:
$( ".LocationOfYourButtonInTheDom" ).on( "click", "ThingBeingClicked", function() {
//code here
});
Edit:
ThingBeingClicked would be the type of DOM object triggering the event, in your case this would be an "a" tag
You can read more about events and delegation from the jquery "on" documentation.

How to get id of clicked div jquery

I have been trying to get the id of a clicked div of same classes but with different IDs. I tried everything I know.
Here is one code I tried (in this one, the .click function() does not work .. edit: meaning it does not seem to run the code when clicked at all!):-
$(".u_search").click(function() {
var attr_u_search = $(this).attr('id');
var dataString = '&u_search=' + $(".u_search").attr('id');
alert(dataString);
$.ajax({
type: "POST",
url: '/script/profile.php',
data: dataString,
cache: false,
success: function(data) {
$('#ui_profile').show();
$('#ui_profile').html(data);
location.hash = 'profile' + 'id=' + dataString;
$(".searchbox").val('');
$("#usr_suggest").hide();
}
});
});
PHP:-
echo "<tr id='" . $id . "' class='u_search' height='40px'><td width='40px'><img class='avatar' src='$avater' /></td><td>" . $fname_ . " " . $lname_ . "</td></tr>";
}}
Here is another combination of codes I tried (error in this one: suppose I have 5 divs, and even if I clicked the 2nd div or the 3rd div, it only captures the id of the first div [div 1] and not the clicked div. I want to be able to capture the id of the clicked div.):-
$(".u_search").click(function() {
var attr_u_search = $(".u_search").attr('id');
var dataString = '&u_search=' + $(".u_search").attr('id');
alert(dataString);
$.ajax({
type: "POST",
url: '/script/profile.php',
data: dataString,
cache: false,
success: function(data) {
$('#ui_profile').show();
$('#ui_profile').html(data);
location.hash = 'profile' + 'id=' + dataString;
$(".searchbox").val('');
$("#usr_suggest").hide();
}
});
});
PHP:-
echo "<tr id='" . $id . "' class='u_search' height='40px' onclick='javascript:op_prof(1)'><td width='40px'><img class='avatar' src='$avater' /></td><td>" . $fname_ . " " . $lname_ . "</td></tr>";
}}
edit: when I use the first code (the one with .click function()) the code does not seem to run at all! .. I am using jquery library version 1.9.1
Inside your function change
$('.u_search').attr('id')
To
$(this).attr('id')
The quotes are important, but this is not inside quotes ;)
When you do
$(".u_search").click(function() {...
your callback function has now an object called this. You can use it to get the actual object you've clicked. So in this case, if you want to get the ID of the clicked object, simply do this:
var the_id = $(this).attr("id");
Inside a function, you can reference to the active jquery object by using $(this)
$(".u_search").click(function() {
...
//get ID of button being clicked upon
//Jquery Solution
$(this).attr('id');
//Or Javascript
this.id;
...
}
$("div").on("click",function(){
var id = this.id; //returns id
});
var attr_u_search = $(this).attr('id');
var dataString = '&u_search=' + $(".u_search").attr('id');
You're selecting every u_search div again to build your datastring, instead of using the already fetched id:
var attr_u_search = $(this).attr('id');
var dataString = '&u_search=' + attr_u_search;
i think it's : $(this).attr('id')
You need use $(this) to have the object that clicked..
var attr_u_search = $(this).attr('id');
var dataString = '&u_search=' + attr_u_search; // it's better !
Try this :
$('body').on('click', '.u_search', function() {
var attr_u_search = $(this).attr('id');
var dataString = '&u_search=' + attr_u_search;
alert(dataString);
// REST OF CODE
});
(Tested and working, if it still does not work, the issue might be somewhere else)
$('body').delegate('.u_search', 'click', function() {
var attr_u_search = $(this).attr('id');
var dataString = '&u_search=' + attr_u_search;
alert(dataString);
});

there is something wrong with this Jquery post?

im trying to pass on the id attribute to the file.php, but its giving me 0 every time, when i try to insert it into the database, the javascript and the html is provided!
$(function() {
$(".follow").click(function(){
var element = $(this);
var I = element.attr("id");
var info = 'id=' + I;
$.ajax({
type: "POST",
url: "file.php",
data: info,
success: function(){}
});
$("#follow"+I).hide();
$("#remove"+I).show();
return false;
});
});
html file:
<div id="follow1"><span class="follow_b"> Follow </span></div>
p.s. it deos insert the value in the database
file.php:
<?php
$id =$_POST['id'];
msql_insert.........
?>
It may not matter in this case, but the ID of an element is not supposed to start with a number.

Categories