2013-02-28 83 views
0

我正在爲一個網站寫一個實時聊天功能,除了顯示輸入的文本之外,我有一切工作,我可以讓我的默認語句顯示在聊天框中,但是我必須具有這樣的功能這裏一個完全獨立的頁面功能如何在php文件2中調用一個函數,而不必在文件二中包含文件1?

if(isset($_POST['method']) === true && empty($_POST['method']) === false) 
    { 
     $method = trim($_POST['method']); 

     if($method === 'fetch') 
     { 
      $messages = fetchMessages(); 

      if(empty($messages) === true) 
      { 
       echo 'A representative will be with you shortly'; 
      }else 
      { 
       foreach($messages as $message) 
       { 
        $ts = $message['timestamp']; 
        ?> 
        <div class = "message"> 
         <a href = "#"><?php echo date('n-j-Y h:i:s a', $ts); ?> 
         <?php echo $message['username']; ?></a>says:<p> 
         <?php echo nl2br($message['message']); ?></p> 
        </div> 
        <?php 
       } 
      } 
     } 
    } 

我打電話,當然在這個文件中的fetchMessage()功能。然而,爲了使這個功能起作用,我必須在chatRoom.php文件中包含它,以便我可以將會話ID傳遞給它,以便它只抓取引用該特定會話的聊天。

如果我將這個腳本移動到chatRoom.php文件或在這個文件中包含chatRoom.php文件,聊天室就會中斷並在聊天框中顯示它自己的第二個副本,聊天框應該發生並且會發生做任何事情。

如果我使用此代碼將fetchMessage移動到文件中,我只獲取默認消息,而不會顯示其他任何內容。

我正在使用jquery和ajax來爲聊天增量提取,該提取存儲在我的數據庫的表中。如果您需要任何其他信息,請詢問我很樂意添加到此,而不確定需要什麼。我對jquery和ajax仍然很陌生。

確定這裏是爲chatRoom.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <?php 
    session_start(); 
    include 'conection.php'; 

    $name = $_GET['name']; 
    $query = "SELECT * FROM chatSession WHERE user_name = '$name'"; 
    $result = mysql_query($query, $con) or die(mysql_error()); 
    $row = mysql_fetch_array($result); 

    $session = $row['session_id']; 

    function fetchMessages() 
    { 
$get = ("SELECT * FROM chatRoom WHERE session_id = '$session' ORDER BY chat.timestamp ASC"); 
$hold = mysql_query($get, $con); 
$show = mysql_fetch_array($hold); 

return $show; 
    } 

    if(isset($_GET['submitmsg'])) 
    { 
$message = mysql_real_escape_string($_GET['usermsg']); 
$throw = "INSERT INTO chatRoom(session_id, source, message, timestamp) VALUES('".$_GET['id']."', '".$_GET['source']."', '$message', UNIX_TIMESTAMP())"; 

if (!mysql_query($throw,$con)) 
{ 
    die('Error: ' . mysql_error()); 
}else 
{ 

} 
    } 
    ?> 
<head> 
    <title></title> 
    <link type="text/css" rel="stylesheet" href="css/chatStyle.css" /> 
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
</head> 
<body> 
    <div id="wrapper"> 
     <div id="menu"> 
      <p class="welcome"><b>Welcome, <?php echo $row['user_name']; ?></b></p> 
      <p class="logout"><a href="nameSub.php?logout=true&name=<?php echo $row['user_name']; ?>&id=<?php echo $row['session_id']; ?>">Exit Chat</a></p> 
      <div style="clear:both"></div> 
     </div> 
     <div id="chatbox"> 
     </div> 
     <form name = "message" action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "get"> 
      <input type = "hidden" name = "name" value = "<?php echo $_GET['name']; ?>" /> 
      <input name = "id" type = "hidden" value = "<?php echo $row['session_id']; ?>" /> 
      <input name = "source" type = "hidden" value = "<?php echo $row['user_name']; ?>" /> 
      <input name = "usermsg" type = "text" id = "usermsg" size = "63" /> 
      <input name = "submitmsg" type = "submit" id = "submitmsg" value = "Send" /> 
     </form> 
    </div> 
    <script type="text/javascript"> 
     $(document).ready(function() 
     { 
      fetchMessages = function() 
      { 
       $.ajax 
       ({ 
        url:'functions.php', 
        type:'post', 
        data:{method:'fetch'}, 
        success:function(data) 
        { 
         $('#chatbox').html(data); 
        } 
       }); 
      } 

      setInterval(fetchMessages, 5000); 
      fetchMessages(); 
     }); 
    </script> 
</body> 

我希望這有助於

回答

1

移動所需要的功能到外部文件(如functions.php)由都包括在碼文件。

編輯:闡述更多:

PHP文件1:

include('functions.php'); 
$myvar = $_GET['myvar']; // jQuery GET variable 
$othervar = $_POST['myvar']; 

commonFunction($myvar,$othervar); 

PHP文件2:

include('functions.php'); 
$myvar = $_GET['myvar']; // jQuery GET variable 
$othervar = $_POST['myvar']; 

commonFunction($myvar,$othervar); 

的functions.php:

function commonFunction($var1, $var2) { 
    // Do common proccessing here 
} 
+0

然後我碰上將會話ID傳遞給函數的問題,我沒有使用jQuery – 2013-02-28 19:07:05

+0

jQuery沒有涉及。每個PHP文件進行正常處理,並調用'functions.php'中的函數,並將其作爲PHP參數傳遞給它從jQuery收到的處理值 – hexblot 2013-02-28 19:08:09

+0

我會給出一個答案,謝謝 – 2013-02-28 19:10:05

相關問題