2016-05-28 4 views
0

ファイルindex.phpview.phpがあります。 index.phpには、MYSQLテーブルへの挿入データを処理するフォームがあります。 view.phpの機能は、人によって挿入されたデータテーブルをindex.phpに取得することです。私の質問は、 "データが挿入された直後にデータを取得するコード(PHP、JavaScriptなど)はありますか?" index.phpにデータが挿入されている場合は、通知音のようにview.phpで再生音が聞こえることがあります。Javascript/PHPがデータを検出できましたか?

+0

なぜあなたはそれを挿入してもらえませんか?どのフレームワークを使用しているのか、データベースの挿入に使用するコードを知ることが役立ちます。 –

+0

私はデータベースにデータを挿入するためにPHPを使用し、私はちょうどフレームワーク、PHPを使用していません。 –

答えて

0

は、以下の簡単なコードを使用すると、テーブルを作成

ファーストを参照してくださいしている

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 ; 

そして、その後

<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> 

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(); 

ようView.phpを作成します。それから確かにx.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(); 
関連する問題