jQuery Ajax: Cannot highlight text from ajax result - php

I am trying to display all employee name from database and automatically display the newly added name without page refresh. I used setInterval to get the data every 1 second so the displayed data is always updated.
My problem is, I cannot highlight a text from the ajax result because it refresh the ajax result every 1 second. So how can I be able to do that?
setInterval(function(){
$.ajax({
type: 'GET',
url: url+'dashboard/getLastPost',
success: function(data) {
$('.lastPost').html(data);
}
});
}, 1000);

How about
success: function(data) {
$('.lastPost').css("color","red");
$('.lastPost').html(data);
}

Related

Counting clicks on an <a> link in a MySQL database

I have different links on my website which lead to other websites. I want to count the amount of clicks on the different links with my MySQL database.
I thought about doing a redirection over a php file which adds it in the database, but I would prefer to not redirect the user. Is it possible that the user just clicks on the tag gets immediately to the external website, and I can still count the click?
Thanks for you answers,
Till
yes you can run an ajax request and when the request is completed then you can redirect user to where ever you want to.
in the ajax request you will count the clicks and save it to the database.
you can do something like
$("a").click(function(){
$.ajax({url: "scriptthatwillcountclick.php", success: function(result){
window.open('redirect_here');
}});
});
Use ajax.
var data = {link:"value"};
$.ajax({
url : "your_file.php",
type: "POST",
data : data,
success: function(data, textStatus, jqXHR) {
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("error");
}
});
You can run this code inside .click function when the link is pressed.
And in your php file just increment the count.
You can use jQuery Ajax funcionality to accomplish your goal, just create an on click even for your "a" element and execute your Ajax to save the click information on your database.
$('a').on('click', function(){
$.ajax({
method: "POST",
url: "save-my-click.php",
data: { 'extrainfo' : "Some extra info" }
});
});
Do not return false, or event.preventDefault() on your click event, because it will brake the redirection.
UPDATE:
You don't need to set the success and error functions because it is going to redirect anyway and you are not going to handle them.

Jquery Ajax Request sometimes not returning data

On button click data is sent always . But jquery ajax sometimes not returning data. I don't understand the reason behind this.
$(function(){
$.ajax({
url:'assets/get_cart_small_info.php',
type: 'get',
data: 'getsmallcart=true',
beforeSend:function(){
$('.cart_info').html('<img src="img/ring.gif">');
},
success: function(data){
setTimeout(function () {
$('.cart_info').html(data);
}, 2000);
},
error:function(){
$('.cart_info').html('<strong>Oops!</strong> Try again');
}
});
})
Currently creating a shopping cart where on every click cart value increases by 1. Then on next click it returns 2, then on another it returns 3 and so on... But sometimes it is not returning anything on clickin. Now then when I click again it returns 5. That means the previous value is sent already but it didn't show at that time and showing it on next click along with the next value. Please help!

ajax append causes javascript to be executed multiple times

I've created a dynamic table in PHP, where every image in a row has a specific url in the ID.
For example:
When a user clicks on this image, it executes an action. This works fine.
Afterwards, in the second ajax, it's supposed to reload the colorbox with the previous url. This also works, however, the javascript seems to be loaded again (with the new values though)?
$('#cboxLoadedContent img[alt="markmessage"]').live('click', function(){
var returnurl = "<?php echo $_SESSION['returnpage']; ?>";
var markurl = $(this).attr('id');
alert(markurl);
// Do the action
$.ajax({
method:'GET',
url: markurl,
cache:false
});
// Reload colorbox again with previous contenturl.
$.ajax({
type: 'GET',
url: returnurl,
dataType: 'html',
cache: true,
beforeSend: function() {
$('#cboxLoadedContent').empty();
$('#cboxLoadingGraphic').show();
},
complete: function() {
$('#cboxLoadingGraphic').hide();
},
success: function(data) {
$('#cboxLoadedContent').empty();
$('#cboxLoadedContent').append(data);
}
});
});
</script>
Is there any way to PREVENT the javascript from being re-appended to the colorbox? I've tried a few methods (like removing it with DOM), but nothing seems to work...
The javascript may NOT be disabled, it's supposed to process a new url afterwards...
Try to replace live to one in first line:
$('#cboxLoadedContent img[alt="markmessage"]').one('click', function(){
Try the simple click event binding. Here you only bind it to the selected elements, not to new appended elements. If you want the event to be fired only once, use one.
I'm not sure which version of jQuery you are using, but live() is deprecated as of 1.7, you might want to try using on().

Can't Get Detail of a Record/Row Generated uding Jquery Ajax

I'm using jquery and codeigniter and wrote code which produce result using .ajax json and display the result in a div called 'result-table'.
Then from the results, I want to performed another query using the same method to get detail info for each row (trigger by clicking on each row). The following code seems able to get the parameter (assetid), but it can't display the result again to the same div 'result-table'. In short I wanted to get detail info of individual rows that resulted from first AJAX query ..AJAX after AJAX..sorry for my english.
//this function - when user click on a row...it will perform another query using ajax json..and display the result in div id=result_table
$('#getassetinfo').live('click', function() {
assetid=$(this).attr("id_hm");
$.ajax({
url: '<?php echo base_url().'index.php/amx/get_asset_info_json/';?>'+ assetid,
type:'POST',
dataType: 'json',
success: function(output_string){
$('#result_table').append(output_string);
}
}); // End of ajax call
});
thanks
You have errors here: If you have firebug , double check it on console panel
url: '<?php echo base_url().'index.php/amx/get_asset_info_json/';?>'+ assetid,
This should fix it:
url: '/index.php/amx/get_asset_info_json/'+ assetid,

Send AJAX request by clicking a link without redirecting user

I have a web application which features a bunch of different items, which are generated from a MySQL table. As users scroll through it, I want them to be able to click a link next to the item which will insert the request into a MySQL database. Normally, I’d do this by creating a PHP page (which I will do anyways) that grabs the item name & user id from the URI using the $_GET method & inserts it into the table. However, in this case, I don’t want the users to be redirected away from wherever they are. I just want the link to send off the request, and maybe display a small message after it is successful.
I figured jQuery/AJAX would be best for this, but as I’m not too familiar with it, I’m not sure what to do. Any tips are appreciated!
You have to do something like
$('.classofyourlink').click(function(e){
e.preventDefault();//in this way you have no redirect
$.post(...);//Make the ajax call
});
in this way the user makes an ajax call by clicking a link without redirecting. Here are the docs for $.post
EDIT - to pass the value to jQuery in your case you should do something like
$('.order_this').click(function(e){
e.preventDefault();//in this way you have no redirect
var valueToPass = $(this).text();
var url = "url/to/post/";
$.post(url, { data: valueToPass }, function(data){...} );//Make the ajax call
});
HTML
<a id="aDelete" href="mypage.php">Delete</a>
Script
$(function(){
$("#aDelete").click(function(){
$.post("ajaxserverpage.php?data1=your_data_to_pass&data2=second_value",function(data){
//do something with the response which is available in the "data" variable
});
});
return false;
});
See http://api.jquery.com/jQuery.ajax/
$('#my-link').click(function(){
$.ajax({
url: "mypage.php",
context: document.body,
success: function(){
$(this).addClass("done");
}
});
return false;
});
$('.classOfYourLinkToBecliked').click(function(){
$.ajax({
type:'GET',
'url':'yoururl',
data: {yourdata},
processData: false,
contentType: false,
cache: false,
dataType: 'json',
success: function(response){
alert(response);
}
});
});

Categories