0
I want to send the data saved in Localstorage with PHP to a file or to a database not necessarily need to be PHP code need some solution for this
<h2>Likes</h2>
<p id="demo"></p>
<button name="clique" class="btn btn-primary" id="like" type="button">
Likes <span class="badge" id="likeQt"> 0 </span>
</button>
<p id="date" style="display: none;">Data e horário do último like: <span id="dateLike" ></span></p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$().ready(function(){
atualizarSpan();
function dadosClick(){
if(typeof(localStorage)){
let likes= '';
if(localStorage.likes){
likes=JSON.parse(localStorage.likes);
likes={numberLike:Number(likes.numberLike)+1,date:Date()};
localStorage.likes=JSON.stringify(likes);
console.table(JSON.parse(localStorage.likes));
atualizarSpan();
}else{
likes={numberLike:1,date:Date()};
localStorage.likes=JSON.stringify(likes);
console.table(JSON.parse(localStorage.likes));
atualizarSpan();
}
}else{
alert('Navegador não suporta localStorage.');
}
}
function atualizarSpan(){
if(localStorage.likes){
var likeQt=JSON.parse(localStorage.likes);
$("#date").show("slow");
$("#dateLike").html(likeQt.date);
$("#likeQt").html(likeQt.numberLike);
}
}
$("#like").click(function(){
dadosClick();
});
});
</script>
Thanks! But How do I put inside the form and give Submit?
– Ryan Lopes
Let’s say you have a form, and in it an input expecting to receive that value, it’s simple:
$('seu-input').value( localStorage.likes )
, and ready, ta in your form. If you want it automatically without the need for the user to click on Ubmit, you can give a$(seu-form).submit()
, but there is neither why not use ajax in my opinion– andre_luiss
how do I receive the data there from the above code?
– Ryan Lopes
What data? If the
localstorage
, as I mentioned above, it’s just a matter of relating key and value and reading them in your backend– andre_luiss
Yes are the localstorage wanted to know how could I catch them in the backend
– Ryan Lopes
Bro if you do what I said in the answer, you’ll be doing a
POST/GET
in the url you passed as parameter, and in the respective page in the backend is the same:$_POST['likes']
etc.. from there you can get them and do what you want– andre_luiss