Answer the question
In order to leave comments, you need to log in
How to change number in database on click?
Hello, I have a notification button on my site, on top of it is the number of unread notifications.
It looks like this:
The conversation is now about this number. The database has an alerts field that is displayed if it is greater than 0.
The implementation is as follows:
$name = Admin;
$user = R::findOne('user', 'name = ?', array($name));
if (($user['alerts'] > '0') ) {
echo '<div id="alerts">'.$user['alerts'].'</div>';
}
?>
<img src="images/alerts.png" hspace="10" vspace="10" onclick="myAlerts()" class="dropbtn" name="alerts"></a>
Answer the question
In order to leave comments, you need to log in
The myAlerts() function in this implementation (onclick attribute) must send an asynchronous request to the server, which will already write to the database.
Since javascript is executed in the browser after the code is executed on the server, the only way to write to the database is AJAX (there are still web sockets, but this is a more complicated topic).
https://ru.wikipedia.org/wiki/AJAX
The handler for this request can be a dedicated php file, or a route (if you are using a framework)
in myAlerts() somewhere add:
$.post('путь/к/пхп/обработчику').then(function(){
$('#alerts').html("0");
});
$name = 'Admin' // Получить имя из сессии или еще откуда
R::exec( 'UPDATE users SET alerts=0 WHERE name = ?', array($name) ); //Как то так, не уверен что это у вас ReadBeanPHP и не уверен что он в exec так получает парамертры
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question