Php ajax显示代码而不是结果

Php ajax显示代码而不是结果,php,ajax,Php,Ajax,我有一个基本的ajax应用程序,它不会工作,而是php代码显示在浏览器中。javascript和html看起来不错。我已经从这里逐字复制了代码: 这是php: <? session_start(); //start the session $cmd = $_GET["cmd"]; $con = mysql_connect('localhost', 'root', 'indosat'); if (!con) { die('Connection to MySQL server fai

我有一个基本的ajax应用程序,它不会工作,而是php代码显示在浏览器中。javascript和html看起来不错。我已经从这里逐字复制了代码:

这是php:

<?
session_start();
//start the session
$cmd = $_GET["cmd"];
$con = mysql_connect('localhost', 'root', 'indosat');
if (!con) {
    die('Connection to MySQL server failed: ' . mysql_error());
    //show error message (mysql_error) if connection failed
}
mysql_select_db('ajax', $con);
//select the database, in this case our database name is "ajax"
if ($cmd === 'GetEmployee') //set command value (executed from javascript ajaxlib.js)
{
    sleep(10);
    //give delay about 10 seconds before execute the command
    //we use this sleep function so we can see the loading animation
    //you can edit/remove
    echo "<table border='1' width='100%'>
    <tr>
    <th>EmpNo</th>
    <th>fName</th>
    <th>lName</th>
    <th>Age</th>
    </tr>";
    //print a table to browser to show the values
    $sql = "select * from employee";
    //this is query to show all records
    $result = mysql_query($sql);
    //execute the query & fill it to $result variable
    while ($row = mysql_fetch_array($result)) {
        echo "<tr>
    <td>" . $row['IdEmp'] . "</td>
    <td>" . $row['fName'] . "</td>
    <td> " . $row['lName'] . "</td>
    <td>" . $row['Age'] . "</td>
    </tr>";
        //print the record to the table
    }
    echo '</table>';
    //close the table
}
mysql_close($con);
//close the mysql connection    
?>

我看不出有什么问题


编辑:它不是短标签。它们已启用,使用“长”标记没有任何区别。

也许您没有启用php短标记?完整的php on标记是
“通常,当您的web服务器不能正确处理php代码时,会发生这种情况。它不是处理代码,而是将原始文件发送到浏览器。问题很可能不在代码中,而是服务器设置。

我打赌短标记就是问题所在。

文件扩展名正确(默认情况下,PHP只处理.PHP文件,除非它们被用作包含)?文件是否位于正确的位置(以便预处理器可以访问它)?

这是未启用的PHP短标记。

我不知道答案,但以下是如何解决这些问题的指南:

  • 完全删除该文件
    • 创建一个同名的新文件,只包含html
    • 只需一个

  • 我怀疑这与PHP有关,并且相信它与Apache有关。你确定你将文件保存在了正确的位置吗?你的
    DocumentRoot
    目录设置是什么?你将文件保存在哪里了?以上所有步骤都有效吗?我怀疑你是否达到了(3),在测试用例中一切正常,但原始代码中断。这可能与文件扩展名有关(在Linux上区分大小写)或者您将文件放在哪里。

    其他php页面工作正常,然后要检查差异。除非web服务器正确传递页面时出现某种问题,否则您无法将“原始”代码发送到浏览器。在我这端,有些奇怪的引号符号不起作用,并显示为currency符号或类似符号,但我原以为我已将它们全部删除。你看到任何奇怪的字符吗?我在代码本身中没有看到任何东西,但我怀疑这是问题所在。这(除非干扰开始和结束标记)应该会产生一个处理错误,这不会导致浏览器显示代码。是的,事实上,对她的php页面,做同样的事情,Fine并没有解决问题,但有深刻的答案。+1它似乎是文件中的字符,有一个引号或一些东西没有被读取,因为它是一个不同的字符集,但我找不到。如果是字符集问题,请查看“file”命令,该命令将识别字符集,而“iconv”将转换字符集。我的钱花在一些简单的事情上,比如将文件保存为“.PHP”而不是“.PHP”“。在浏览器中访问php页面page.php?cmd=GetEmployee时,应检查该页面是否正确返回结果