i have problem with my rate, my code like below
$(function(){
var href = jQuery(location).attr('href');
$('.rate-it').rating({
required: true,
callback: function(value, link){
$.ajax({
type: "POST",
url: "<? echo base_url('post/rate/'); ?>",
dataType: "json",
data: "&postlink=" + href + "&ratevalue=" + value,
success: function(msg){
$('.rate-it').attr('disabled', true);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
displayError();
}
});
},
how to set people to can not vote repeatedly?
i'm using codeigniter
$(function(){
var href = jQuery(location).attr('href');
$('.rate-it').rating({
required: true,
callback: function(value, link){
$.ajax({
type: "POST",
url: "<? echo base_url('post/rate/'); ?>",
dataType: "json",
data: "&postlink=" + href + "&ratevalue=" + value,
success: function(msg){
$('.rate-it').attr('disabled', true);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
displayError();
}
});
},
focus: function(value, link){
var tip = $('#rate-result');
tip[0].data = tip[0].data || tip.html();
tip.html(link.title || 'value: '+value);
},
blur: function(value, link){
var tip = $('#rate-result');
$('#rate-result').html(tip[0].data || '');
}
});
});
If you want someone not to be able to vote repeatedly, you have to disable the button AND check on the server side that this client didn't vote yet.
Otherwise one could intercept the ajax request and repeat it.
You can try it :
In firefox or chrome, browse to your site, type F12, go to the console menu and copy the following :
$.ajax({
type: "POST",
url: "[your url here]",
dataType: "json",
data: "&postlink=" + href + "&ratevalue=" + value,
});
Launching it with ctrl + enter, you send a vote request.
Now put that in a for loop, and you have tons of votes.
Globally, keep in mind that you never clean user input on the client side.
Related
In my ajax script below when I do console.log for duration it returns <select name="duration" id="duration"> instead of the value. I think for this reason my function is not working properly. I am not getting any response back from the PHP page and the loading image keeps on loading.
$(document).ready(function() {
$("#submit").click(function() {
var dataString = {
quantity: $("#quantity").val(),
duration: $("select[name='duration']").val()
};
console.log(duration);
$.ajax({
type: "POST",
dataType : "json",
url: "rent-process.php",
data: dataString,
cache: true,
beforeSend: function(){
$("#submit").hide();
$("#loading-rent").show();
},
success: function(json){
$("#loading-rent").hide();
$("#submit").show();
$(".message").html(json.status).fadeIn();
$('#wallet-av').html('$' + json.wallet);
$('#ref-av').html(json.referrals);
}
});
return false;
});
});
var a = $('.level').find(":selected").attr('value');
var b = $('.category').find(":selected").attr('value');
var c = $('.topic').find(":selected").attr('value');
var d = $('.question').val();
$.ajax({
url: "http://localhost/2016/2016_02_15_Quiz/index.php/Admin/getthecompr/" + a + "/" + b + "/" + c,
type: "GET",
dataType: 'json',
data: '',
success: function(data) {
var id = data;
$.ajax({
url: "http://localhost/2016/2016_02_15_Quiz/index.php/Admin/insert4/" + id[0]['id_comprehens'] + "/" + d,
type: "GET",
dataType: 'json',
data: '',
success: function(data) {
alert("Success");
},
error: function() {
alert("Fail")
}
});
},
error: function() {
console.log('Paastoge');
}
});
});
I got trouble making this nested-JQuery-Ajax. In the first Ajax, I take the value of compre_id that I want to use in second Ajax. In the second Ajax, I will insert the id_comprhensive and question in MySQL table. The Problem is, inserting data to SQL table is success. But, instead of displaying "success", fail alert does appear. Is there something problem with this code ?
I need the content of the Variable data from this AJAX Request but in a PHP Variable.
Is that possible?
$.ajax({
type: 'POST',
async: false,
url: '/php/checkAccountData.php',
data: 'un=' + PasswordResetUsernameText + '&e=' + PasswordResetEmailText,
success: function(data){
var responseanswer = data;
alert(responseanswer);
setTimeout(function(){
$("img[name=UserNameEmailText]").fadeTo(500,0);
$("div[id=PasswordResetLoadingDiv]").fadeOut(500)}, 1500);
setTimeout(function(){
$("img[name=UserNameEmailComplete]").fadeTo(500,1)}, 2500);
setTimeout(function(){
$('MainFrame').fadeTo(500,1);
$('#LoginField').fadeOut(500, "swing")}, 7000000);
}
I have a script
$('#postinput').on('keyup',function(){
var txt=$(this).val();
$.ajax({
type: "POST",
url: "action.php",
data: 'txt='+txt,
cache: false,
context:this,
success: function(html)
{
alert(html);
}
});
});
Suppose someone types a ,ajax runs . Immediately he types b c and so on. Ajax runs everytime. Is there a way to stop previous request when new is made ?
You can use the abort() method of the xhr. Try this:
var currentXhr;
$('#postinput').on('keyup',function(){
currentXhr && currentXhr.readyState != 4 && currentXhr.abort(); // clear previous request
var txt = $(this).val();
var currentXhr = $.ajax({
type: "POST",
url: "action.php",
data: 'txt=' + txt,
cache: false,
context:this,
success: function(html) {
alert(html);
}
});
});
If you don't want to use a global variable, you could store the xhr in a data-* attribute of the #postinput element.
Another method is to only fire the AJAX request when the user has stopped typing:
var timer;
$('#postinput').on('keyup',function(){
clearTimeout(timer);
var txt = $(this).val();
var timer = setTimeout(function() {
$.ajax({
type: "POST",
url: "action.php",
data: 'txt=' + txt,
cache: false,
context:this,
success: function(html) {
alert(html);
}
});
}, 100); // sends request 100ms after typing stops.
});
You can do like this with Jquery-
jaxRequests = new Array();
queueRequest = function(whatever, you, want) {
if(ajaxRequests[ajaxRequests.length - 1]) {
ajaxRequests[ajaxRequests.length - 1].abort();
}
ajaxRequests[ajaxRequests.length] = //Insert New jQuery AJAX call here.
}
I'm creating a CMS using jQuery and AJAX. When I click, my "Add Campaign" buttom, it creates a unique client ID in the DB and on a hard reload, the new client shows up in its container. I am trying to use ajax to reload the container on the fly and I'm not having the exact luck i am hoping for. I can get it to reload, but it's like it's pulling in descriptions of each of the clients as well!
function AddNewClient() {
dataToLoad = 'clientID=' + clientID + '&addClient=yes';
$.ajax({
type: 'post',
url: '/clients/controller.php',
datatype: 'html',
data: dataToLoad,
target: ('#clientssidebar'),
async: false,
success: function(html){
$('#clientssidebar').html(html);
},
error: function() {
alert('An error occured!');
}
});
};
Try:
function AddNewClient()
{
dataToLoad = 'clientID=' + clientID + '&addClient=yes';
$.ajax({
type: 'post',
url: '/clients/controller.php',
data: dataToLoad,
success: function(html){
$('#clientssidebar').html(html); },
error: function() {
alert('An error occured!'); }
});
}