-1

I have a basic ajax application, which will not work, instead the php code is displayed in the browser. The javascript and html seem fine. I have copied the code verbatim from here:

http://arief.aeroven.com/2008/07/30/first-ajax-script-tutorial-connecting-ajax-contain-pure-htmlphpand-javascript-to-mysql-database/

and this is the 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    
?>

I don't see what the problem could be

edit: it is NOT the shorttags. they are enabled, andusing "long" tags makes no difference.

step
  • 2,254
  • 2
  • 23
  • 45
Joshxtothe4
  • 4,061
  • 10
  • 53
  • 83
  • You should check if the page is result is returned correctly when simply visiting the php page in your browser, page.php?cmd=GetEmployee – Pim Jager Nov 17 '08 at 17:07

6 Answers6

4

Perhaps you don't have php short tags enabled? The full php on tag is "<?php" Is the file extension one that is setup to be parsed by php?

DreamWerx
  • 2,888
  • 1
  • 18
  • 13
3

Typically this happens when your web server doesn't handle the PHP code correctly. Rather than processing the code, it's sending the raw file to the browser. The issue is most likely not in the code, but the server setup.

Harper Shelby
  • 16,475
  • 2
  • 44
  • 51
  • other php pages work without a problem –  Nov 17 '08 at 15:10
  • Then the thing to check is the differences. There really isn't any way that you get 'raw' code to the browser unless there's some sort of issue with the web server passing the page properly. – Harper Shelby Nov 17 '08 at 15:13
  • There were some strange quote symbols that did not work on my end and display as currency symbols or similar, but I had thought I had removed them all. Do you see any odd characters? –  Nov 17 '08 at 15:16
  • I don't see anything in the code itself, but I doubt that's the issue. That (barring interference with the start and end tags) should yield a processing error, which would *not* result in code being displayed by the browser. – Harper Shelby Nov 17 '08 at 19:04
1

Has the file correct extension (PHP processes only .php files by default unless they are used as include)? is the file in correct location (so the preprocesor can reach it)?

Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
0

I would bet on the short tags being the issue

Adam Lerman
  • 3,369
  • 9
  • 42
  • 53
0

it is the php short tags not being enabled.

Ronald Conco
  • 855
  • 7
  • 11
0

I don't know the answer but here's a guide on how to solve these issues:

  1. completely remove the file
    • create a new file with the same name, containing just html
    • rewrite the contents with just a <?php echo phpinfo(); ?>

I doubt this has anything to do with PHP and believe it's related to Apache. Are you sure you saved the file in the right place? What is your DocumentRoot directory setting and where have you saved the file? Do all of the above steps work? I doubt that you reach (3), everything works fine in the test case but the original code breaks. It's probably got something to do with the file extension (on Linux it's case-sensitive) or where you've placed the file.

Tom
  • 6,991
  • 13
  • 60
  • 78
  • it seems to be the characters in the file, there is a quote or something that is not being read because it is a different character set, but I cannot find it. –  Nov 17 '08 at 15:43
  • If it's a character set issue, check out the "file" command which will identify character set and "iconv" which will convert character sets. – rmeador Nov 17 '08 at 15:53