Ajax displaying code instead of result

I have a basic ajax application that will not work, PHP code is displayed in the browser instead. Javascript and html seem to be fine. I copied the code verbatim:

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

and this is 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 might be

edit: these are NOT short. they are included, and the use of "long" tags is irrelevant.

0


source to share


6 answers


I don't know the answer, but here is a guide to solve these problems:

  • completely delete the file
    • create a new file with the same name containing only html
    • rewrite content with just <?php echo phpinfo(); ?>



I doubt it has anything to do with PHP and believe it has something to do with Apache. Are you sure you saved the file in the correct location? What is your setting DocumentRoot

and where did you save the file? Are all these steps followed? I doubt you have achieved (3), everything works well in the test case, but the source code breaks. It probably has to do with the file extension (on Linux it is case sensitive) or where you placed the file.

0


source


Perhaps you don't have php private tags? The complete php tag on "<?php"

Is the file extension the one that is configurable for php?



+4


source


This usually happens when your web server is not handling PHP code correctly. Instead of processing the code, it sends the raw file to the browser. The problem is most likely not in the code, but in the server configuration.

+3


source


Does the file have the correct extension (PHP only handles .php files by default if they are not used as include)? is it the file in the right place (so the preprocessor can reach it)?

+1


source


I would bet on short tags being the problem

0


source


these are short short php tags.

0


source







All Articles