S
S
sanya1642017-07-13 14:24:54
PHP
sanya164, 2017-07-13 14:24:54

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:

961d33ef0da14bcf9909768da9703826.PNG
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:

dfc1e84d6e3d4c998791b676842beb05.PNG

$name = Admin;
$user = R::findOne('user', 'name = ?', array($name));


  if (($user['alerts'] > '0') ) {

    echo '<div id="alerts">'.$user['alerts'].'</div>';
  }
?>


How can I make it so that when the alert button is clicked, the number in the alerts field becomes 0 ?

Here is the code for the alert button, now when clicked, it shows notifications using javascript.

<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

3 answer(s)
V
Vladislav Ivasik, 2017-07-13
@sanya164

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)

A
Alexander Kuznetsov, 2017-07-13
@DarkRaven

in myAlerts() somewhere add:

$.post('путь/к/пхп/обработчику').then(function(){
 $('#alerts').html("0");
});

In the handler:
$name = 'Admin' // Получить имя из сессии или еще откуда
 R::exec( 'UPDATE users SET alerts=0 WHERE name = ?', array($name) ); //Как то так, не уверен что это у вас ReadBeanPHP и не уверен что он в exec так получает парамертры

M
MherArsh, 2017-07-13
@MherArsh

in the myAlerts() function, do an Update with a username that you can take from the session.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question