網頁

2013年9月26日 星期四

jQuery load php MySQL

index.php
 






Your Message is loading..

data.js
 

  var  server_url = location.href.replace(location.pathname, ""); //http://localhost
 // alert(server_url);
  var msg;  var url_php;
  var auto_refresh = setInterval(
  
 function ()
 {

   //http://localhost/php/chat/db/chat_db.php  
          url_php =  server_url+"/php/chat/db/chat_db.php";
  
    
     var msg = input; //from index.php
     var getData =  'msg='+ msg ;     
    
        $.ajax({
             url: url_php,
             datatype: "json",
             data: getData,
             type: 'get',
              success: function (response) {   
                
             var message= response["message"];
                   // alert(message);
                   // console.log(message);
                     document.getElementById('msg_id').innerHTML = message;
       
              },
         error: function(response){
        //alert("error!");
         }
      });
  
      
 }, 500); // refresh every 500 milliseconds

chat_db.php
 


  $msg=null;
  $msg = strtolower($_GET['msg']);
  
  receive_msg($msg);


  function receive_msg($msg)
  {
    
 header("Content-type: application/json");
        //require_once("db.config.php");
       require_once '../../db.config.php';

     $dbh=null;
          $dbh = new PDO("mysql:host=" . wp_db('hostname') . ";dbname=" . wp_db('dbname'), wp_db('username'), wp_db('password'),   array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") ); //POD處理中文的顯示的問題

 
         $sql_table = "SELECT * FROM yourTable ORDER BY id DESC LIMIT 1"; //監聽抓最新一筆

         foreach ($dbh->query($sql_table) as $row_table) {

          $message =   $row_table['message'];
       
          }      


          echo json_encode(
            array(                         
                    "message" =>$message                    
            )
          );
    
   }  


沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。