How to empty the GET values from a MySQL/jQuery ajax call - php

If you do an ajax call to retrieve info from a MySQL database and you time the function so it runs every X seconds, I am a bit confused about where do the GET values reside, in order to empty them.
I have this code in test.js:
var fill = function () {
$.ajax({
type: "GET",
url: "refresh.php",
dataType: "html",
success: function( data ) {
$( "#tb" ).append( data );
setTimeout( fill, 5000);
}
});
} ;
and in refresh.php I have the SQL query to retrieve the info. After the query, I setting the table like this:
printf( "<tr><td>".$row['name'].
"</td><td>".$row['surname'].
"</td><td>".$row['phone'].
"</td><td>".$row['date'].
"</td></td></tr>");
How can I stop the table from populating the same row every 5 seconds? I can pass the $row['name'] values to regular php values if needed, but I don't fully get in which moment I should empty them.

There might be another solution to this, but I think that my problem could be easily solved by removing the contents of the table before adding another. For instance:
var fill = function () {
$.ajax({
type: "GET",
url: "refresh.php",
dataType: "html",
success: function(data) {
$('#tb').empty().append(data);
}
});
};
setInterval(fill, 5000);
You can also make use of setInterval() so that you don't need to define a timeout on every execution.
That will do.
Thanks!

Related

How to get and store Twitter share counts in database?

I want to get twitter counts of my website and article pages. After getting them I want to store them in database. I tried getting counts and posting them on my query.php using ajax but its showing me Cross Origin Block Error. Is there any way to do this? or am I missing something?
$.ajax({
url: "https://cdn.api.twitter.com/1/urls/count.json?url=<?php the_permalink(); ?>",
crossDomain: true,
crossOrigin: true,
success: function(data) {
var count = JSON.parse(data);
$("#dd").html(count);
$.ajax
({
type: "POST",
url: "post.php",
data: { stats: count, paralink:'<?php echo $rows['link_id'];?>', social:'2' },
success: function(data) {
},
dataType: 'json'
});
}
});
It would probably be more efficient / faster if you just made one ajax call to your server and a request to twitter directly from your php script.
And it would solve your problem as well:
// you should validate the input, but for simplicity:
$link = $_POST['paralink'];
// ^^^^^^^^ I am not sure if this is the page you want, you need to check that.
// get twitter json
$json = file_get_contents('https://cdn.api.twitter.com/1/urls/count.json?url=' . $link);
Now you just need the inner ajax call.

Make ajax call to send data onclick event in div

I have a div, which shows Rate me section. i.e. Five stars. When the user click on it, the rating value gets prompted.
<div class="rateit bigstars" id="rateit99" data-rateit-starwidth="32" data-rateit-starheight="32" onclick="alert($('#rateit99').rateit('value'))">
</div>
In here rather than an alert, I want to make an ajax call, which sends(post data) Rated value to data.php file.
How can it be done?
I don't understand what's on your HTML code, but i can understand what you are trying to achieve.
First clean your HTML code to make it easier to read for the purpose.
Here is an example of how you can receive the data and pass it using ajax
// Use the id of your trigger element
$('#submit').click(function() {
// Make your datastring (you can get other values with var a = $('#id').val();
var dataString = 'value1='+ value1 + '&value2=' + value2 ;
$.ajax({
type: "POST",
url: "data.php",
data: dataString,
cache: false,
success: function(data)
{
// handle the result
}
});
});

Get response from ajax post form call when using data which is displayed using AJAX

I have a dropdown box, and when it changed, i will display a table containing information in the database using JQuery. Now that one is working good. In the same time, I want to apply the "edit in place" to that table. When I clicked a row, then I am able to change the value of the row,however, the data is not change when I submit the new value. I have to refresh my page and then the value will be updated. I suspect it is because of the table which is called using JQuery. But here is the codes. I am using this function to display the table of information:
function loadData(page){
var dataString = "";
dataString = "page="+page;
$.ajax({
type: "POST",
url: "load_data.php",
data: dataString,
success: function(msg){
$("#container").ajaxComplete(function(event, request, settings)
{
$("#container").html(msg);
});
}
});
}
I am using this code to apply "edit in place" which is currently working, but the only thing which is not working is when I want to reload the table with the new value.
$(".edit_tr").live('click',function(){
var ID=$(this).attr('id');
$("#first_"+ID).hide();
$("#first_input_"+ID).show();
var first=$("#first_input_"+ID).val();
}).live('change', function(){
var ID=$(this).attr('id');
var first=$("#first_input_"+ID).val();
var dataString = 'id='+ ID +'&thename='+first;
var initName = $("#hidden_first_input_"+ID).val();
$.ajax({
type: "POST",
url: "edit_page.php",
data: dataString,
cache: false,
success: function(html){
alert(html); // the alert is not coming. even put any process, nothing happen
}
});
});
But the values is updating into the database. I have to refresh, and call the table one more time using JQuery then the value will be updated. Or I can click the paging, then it will be correct. ANy idea why is this happening? Thank you in advance!

sending increment value from jquery to php file with for loop help

I have this for loop in jquery
$(document).ready(function() {
for(i=0; i<counter; i++)
{
dataCounter = i;
$.ajax({
url: 'file.php',
dataType: 'json',
data: dataCounter,
error: function(){
alert('Error loading XML document');
},
success: function(data){
$("#contents").html(data);
}
});
}
});
And then I want to bring in my dataCounter into the file.php as a variable and have it change each time so I can get different records in mysql in file.php, am I doing this right? How would the php portion look like? I know how pass variables to a php file with this method if I had a form, but I don't have a get or a post form to work with. Also, my variables are going to change.
Can someone help me? Thank you!
While I don't recommend running an ajax query inside of a loop, I am wiling to explain the data option for $.ajax(). Ideally, you pass an object as the data option, and it is translated by jQuery into a query string where each object property name is a key and its value is the value:
data: {
count: dataCounter
}
becomes
?count=1
in the query string of the ajax request if datacounter is equal to 1.
In PHP you would access it as $_GET['count'].
data needs to be a key-value pair, not just a value as you have it here. Try something like: (not tested)
$.ajax({
url: 'file.php',
dataType: 'json',
data: ({dataCounter : dataCounter}),
error: function(){
alert('Error loading XML document');
},
success: function(data){
$("#contents").html(data);
}
});

Get multiple values through an AJAX query

Hey guys, I am very new to AJAX and and working on a rating script, but I want to be able to pass multiple values back into my ajax function :
Right now it runs a to a php script called ratings, where it takes the total value of votes / votes and multiplies it by the width of each start to get an accurate current rating. What I'd like to do is also pass back the amount of votes so I can display them. I know how I could do this by making another function but that seems redundant and not very efficient.
My question is, is it possible to pass back not only the width (value / votes * 22) for my rating box, but also the total # of votes in 1 query. If not the better question would it be better to pass back a string in jquery that already has the votes & value, and do the width calculation with java script ?
$(document).ready(function() {
getRating();
function getRating(){
$.ajax({
type: "GET",
url: "../includes/rating.php",
data: "action=get&bookid="+$("#current").attr("value"),
cache: false,
async: false,
success: function($rating) {
$("#current").css({ width: "" + $rating });
},
error: function(result) {
alert("Error");
}
});
}
Thanks!
Yes you can pass back both values. Just send JSON using json_encode instead of text.
$(document).ready(function() {
getRating();
function getRating(){
$.ajax({
type: "GET",
dataType: 'json',
url: "../includes/rating.php",
data: "action=get&bookid="+$("#current").attr("value"),
cache: false,
async: false,
success: function(data) {
$("#current").css({ width: "" + data.rating });
$("#votes").html(data.votes);
},
error: function(result) {
alert("Error");
}
});
}
If your struggling to passback multiple values in one callback you could try inserting a random character (like a #, something your not going to use) inbetween the values then split it on the server side. For example.
Client Side...
var callback = value1##value2##value3;
Server Side...
$values = split($callback, "##");
$value1 = $values[0];
$value2 = $values[1];
Hope that helps...
Since the width is a derived value based on the number of votes, I would pass the number of votes and do the math in Javascript.

Categories