Up
jQuery - Ajax - Lottery Winner
Hi people.
I'd like to do a lottery system, with a countdown and a winner that win points. I did all the countdown and correctly did the ajax requests for picking up a winner and put his points in the database :
jQuery(function ($) {
var launch = new Date(2017, 10, 06, 23, 41, 40);
var days = $('#jours');
var hours = $('#heures');
var minuts = $('#minutes');
var seconds = $('#secondes');
function setDate(){
var now = new Date();
var time = launch.getTime() - now.getTime();
var s = (launch.getTime() - now.getTime()) / 1000;
var d = Math.floor(s / 86400);
var timer = setTimeout(setDate, 1000);
days.html('<strong class="min">'+ d +'</strong><p class="arvo">Jour'+(d>1?'s':'')+'</p>')
s -= d * 86400;
var h = Math.floor(s / 3600);
hours.html('<strong class="min">'+ h +'</strong><p class="arvo">Heure'+(h>1?'s':'')+'</p>')
s -= h * 3600;
var m = Math.floor(s / 60);
minuts.html('<strong class="min">'+ m +'</strong><p class="arvo">Minute'+(m>1?'s':'')+'</p>')
s -= m * 60;
s = Math.floor(s);
seconds.html('<strong class="min">'+ s +'</strong><p class="arvo">Seconde'+(s>1?'s':'')+'</p>');
if(Math.ceil(time) <= 1000)
{
$.ajax({
url: "/triumphclick/get_user",
type: "GET",
dataType: "json",
success: function ( data ) {
var user = data;
$('.gagnant').html('<strong class="win">Le gagnant est '+ user.login +'!</strong>' +
'<p><h6 class="org"><i><b>Votre compte a été crédité ! Félicitation !</b></i></h6></p>')
}
});
clearTimeout(timer);
$('.bloc-min').addClass('removed');
}
}
setDate();
});
I'm getting the user with this laravel route :
Route::name('get_user')->get('get_user', function(){
$lien = User_liens::where('chance', true)->inRandomOrder()->first();
$user = \App\User::where('id', $lien->user_id)->first();
$solde_pts = $user->solde_points + 20;
$user->update([
'solde_points' => $solde_pts,
]);
return \Response::json($user);
});
But the problem is that when I reload the page, it do again the code and the winner get his points again and again.
And something else, I think that this way is not really secure, it's a js code so I guess people can go through, so I wanted to do this code :
if(Math.ceil(time) <= 1000)
{
$.ajax({
url: "/triumphclick/get_user",
type: "GET",
dataType: "json",
success: function ( data ) {
return data;
}
});
}
On the server side with PHP. But I never did that, and I don't know how to do all the checking and the code, from JS to PHP, I'm really lost here..
Well, I don't know if I was clear, but if you have some ideas that could help me, I thank you !
Please or to participate in this conversation.