tags:

views:

167

answers:

6

Hello,

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.

+3  A: 

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
yes, in fact toher php pages that do the same thing work fine
Joshxtothe4
Didn't fix the problem, but insightful answer. +1
Tom
+3  A: 

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
other php pages work without a problem
Joshxtothe4
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
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?
Joshxtothe4
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
A: 

I would bet on the short tags being the issue

Adam Lerman
+1  A: 

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)?

CommanderZ
My money is on something as simple as having saved the file as ".PHP" instead of ".php".
Tomalak
A: 

it is the php short tags not being enabled.

Ronald Conco
+1  A: 

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

  1. completely remove the file
  2. create a new file with the same name, containing just html
  3. 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
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.
Joshxtothe4
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