如何从php文件中调用php文件中的一个函数,而不必在文件二中包含文件一?

时间:2013-02-28 19:02:16

标签: php jquery ajax

我正在为一个网站编写一个实时聊天功能,除了显示输入的文本外,我还能正常工作,我可以将我的默认语句显示在聊天框中,但是我必须具有执行此操作的功能在一个完全独立的页面上这里是函数

    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 个答案:

答案 0 :(得分:1)

将所需的函数移动到两个文件包含的外部文件(例如functions.php)。

修改:详细说明:

PHP file1:

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

 commonFunction($myvar,$othervar);

PHP file2:

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
}