Yovi Prasetyo Yovi Prasetyo - 6 months ago 19
SQL Question

Can Javascript/PHP detect data was inserted?

I have file

index.php
and
view.php
. In
index.php
there is a form to process some insert data to MYSQL table. The function of
view.php
is to get data table that is inserted by person in
index.php
. My question is "is there any code(php, javascript, etc.) to get the data right after the data was inserted?" and maybe if the data was inserted in
index.php
, we can hear a sound playing in
view.php
like a notification sound.

Answer

below are simple code please refer

First you create a table

CREATE TABLE IF NOT EXISTS `messageTest` (
`id` int(50) NOT NULL AUTO_INCREMENT,
`notification` varchar(255) NOT NULL,
`status` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

Then Create View.php like

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
 <script type="text/javascript" charset="utf-8">

 function addmsg(type, msg){

 $('#notification_count').html(msg);

 }
 function playSound(filename){   
            document.getElementById("sound").innerHTML='<audio autoplay="autoplay"><source src="' + filename + '.mp3" type="audio/mpeg" /><source src="' + filename + '.ogg" type="audio/ogg" /><embed hidden="true" autostart="true" loop="false" src="' + filename +'.mp3" /></audio>';
        }
 function waitForMsg(){

 $.ajax({
 type: "GET",
 url: "select.php",

async: true,
cache: false,
 timeout:50000,

  success: function(data){
 if(data>0){
     playSound("mymp3");
 }
  addmsg("new", data);
  setTimeout(
  waitForMsg,
 1000
 );
},
 error: function(XMLHttpRequest, textStatus, errorThrown){
 addmsg("error", textStatus + " (" + errorThrown + ")");
 setTimeout(
 waitForMsg,
 15000);
}
});
};

 $(document).ready(function(){

 waitForMsg();

 });

 </script>
 <span id="notification_count"></span>
<a href="#" id="notificationLink" onclick = "return getNotification()">Notifications</a>
<div id="HTMLnoti" style="textalign:center"></div>

Then select.php

   $servername = "localhost";
   $username = "root";
   $password = "";
   $dbname = "mydatabaseName";

   // Create connection

   $conn = new mysqli($servername, $username, $password, $dbname);

   // Check connection

   if ($conn->connect_error) {

       die("Connection failed: " . $conn->connect_error);

   } 

   $sql = "SELECT * from messageTest where status = 'unread'";
   $result = $conn->query($sql);
   $row = $result->fetch_assoc();
   $count = $result->num_rows;
   echo $count;
   $conn->close();

Then index.php

   $servername = "localhost";
   $username = "root";
   $password = "";
   $dbname = "mydatabaseName";

   // Create connection

   $conn = new mysqli($servername, $username, $password, $dbname);

   // Check connection

   if ($conn->connect_error) {

       die("Connection failed: " . $conn->connect_error);

   } 

     $sql = "INSERT INTO messageTest (id, notification, status) VALUES (1, 'New notification', 'unread')";
   $result = $conn->query($sql);

   $conn->close();