Apache sends php source code to Ajax

Suse 11.2

Php works normal when file is loaded on browser. But when I read XMLHttpRequest responseText it shows the php source code.

Bug in your code for sure.

Sorry, crystal ball not working so can’t help you without details. Maybe even show us the PHP code, too much to ask?

This tutorial:
Ajax Tutorial - XMLHttpRequest

html:

<html>
<body>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			document.myForm.time.value = ajaxRequest.responseText;
		}
	}
	ajaxRequest.open("GET", "serverTime.php", true);
	ajaxRequest.send(null); 
}

//-->
</script>



<form name='myForm'>
Name: <input type='text' onChange="ajaxFunction();" name='username' /> <br />
Time: <input type='text' name='time' />
</form>
</body>
</html>

php:

<?php
echo date("H:i:s"); 
?>

And also some other code I tried. Simple as anything, so I think it must be server configuration problem? But what?

Works fine for me if you move <body> down above <form> and enclose the <script> in <head></head>. JS works better if declared inside <head>.

Yes it works also on another server I tried. So it is a server problem but what? That is why I’m here and not on a php or javascript forum. :\

What you get if you run this piece of code:

<?php
phpinfo();
?>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

If you are getting PHP code when you request that PHP page (the one you
posted originally) then your Apache server is not configured to handle PHP
at all. Make sure you have the mod_php* (or whatever it’s called) module
loaded and that Apache knows to use that module to interpret all .php
files. Looking at my SLES 10 box I have a php.conf files in
/etc/apache2/conf.d which you should probably have as well, and also
/etc/apache2/sysconfig.d/loadmodule.conf has the following line:

LoadModule php5_module /usr/lib/apache2/mod_php5.so

After making sure that is all there verify your Apache configuration:

sudo /etc/init.d/apache2 configtest

and if that returns ‘Syntax OK’ go ahead and restart Apache:

sudo /etc/init.d/apache2 restart

Test.

Good luck.

mmarif4u wrote:
> What you get if you run this piece of code:
>
>
> Code:
> --------------------
> <?php
> phpinfo();
> ?>
> --------------------
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJLTJigAAoJEF+XTK08PnB5Qh8P/0BGu9vdVRRRE/FvibNB04dL
lkditn4Tg0PfGHPTFhaU8Hr6sYY+PrRPNRhcLx80OEnZtlm6o/ZQLZ/wCpdfcgd1
Ua3FE4Pi75cAvK8QXhSDc8hIsNuF6yo15sUXWZ2sfhhIgKurzsug5tvxkHt/9bou
4ZuUBUgbRY9ivjnec1HeQXZgzWEqNp8plwwUhh/JwB7KL2KRch81EpM2LzdyRtWO
D3zdv0c2r4c/p3xeNWsx5wL2n3xyU+33lhGE5TYazXhkbNL56mN9Y8ynwdeWAH1r
GVMswf2hXfNVcuTYtFMuTG2PxBpsEVhYsVNZHymORmHyrLcXcXNSVYB8SqyUBFE0
cwJYQUj0ru1QkFS0/wvXNChbJGd9z1mhQqR+HdsBqImsoEzl3lFX5Q9NB1JMMzt4
olsCmEc76ewZRfJsNqMWIgIcxGsqt0PSDQH2KVKmIi1ykRk9DY0Fr3IdrK9X8knv
sX/eSFtrMVzDcvjtJjMw5lSRTA7irZ6YMV/g7ibQ6ftn95mUDeGmmyuD6ToHb7Pv
IjPr3qHZE2/LvaAZzwTQ8XBbo4EGPJ0BbHcgIgCB0KghbieLF6LS6/9mVDdVrZLM
Tt2xKlhd8artQeMonXQk3nKrGlaKboqqVzrUfzicBmQLOeXs9GNUeW43F8aiVYkT
9ik800Mg1WAFSVazeGvu
=4GT4
-----END PGP SIGNATURE-----

Getting lots of information like:

dom
DOM/XML 	enabled
DOM/XML API Version 	20031129
libxml Version 	2.7.1
HTML Support 	enabled
XPath Support 	enabled
XPointer Support 	enabled
Schema Support 	enabled
RelaxNG Support 	enabled

and

SimpleXML
Simplexml support	enabled
Revision 	$Revision: 272374 $
Schema support 	enabled

SPL
SPL support	enabled
Interfaces 	Countable, OuterIterator, RecursiveIterator, SeekableIterator, SplObserver, SplSubject
Classes 	AppendIterator, ArrayIterator, ArrayObject, BadFunctionCallException, BadMethodCallException, CachingIterator, DirectoryIterator, DomainException, EmptyIterator, FilterIterator, InfiniteIterator, InvalidArgumentException, IteratorIterator, LengthException, LimitIterator, LogicException, NoRewindIterator, OutOfBoundsException, OutOfRangeException, OverflowException, ParentIterator, RangeException, RecursiveArrayIterator, RecursiveCachingIterator, RecursiveDirectoryIterator, RecursiveFilterIterator, RecursiveIteratorIterator, RecursiveRegexIterator, RegexIterator, RuntimeException, SimpleXMLIterator, SplFileInfo, SplFileObject, SplObjectStorage, SplTempFileObject, UnderflowException, UnexpectedValueException

What should I look for?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

If this file where you have the phpinfo() call is in the same directory as
the other file that does not work what is different between them? Are
they the same file? Have the same file extension if not?

Good luck.

kansalliskala wrote:
> mmarif4u;2102989 Wrote:
>> What you get if you run this piece of code:
>>
> Code:
> --------------------
> > > <?php
> > phpinfo();
> > ?>
> --------------------
>
> Getting lots of information like:
>
>
> Code:
> --------------------
> dom
> DOM/XML enabled
> DOM/XML API Version 20031129
> libxml Version 2.7.1
> HTML Support enabled
> XPath Support enabled
> XPointer Support enabled
> Schema Support enabled
> RelaxNG Support enabled
> --------------------
>
>
> and
>
>
> Code:
> --------------------
> SimpleXML
> Simplexml support enabled
> Revision $Revision: 272374 $
> Schema support enabled
>
> SPL
> SPL support enabled
> Interfaces Countable, OuterIterator, RecursiveIterator, SeekableIterator, SplObserver, SplSubject
> Classes AppendIterator, ArrayIterator, ArrayObject, BadFunctionCallException, BadMethodCallException, CachingIterator, DirectoryIterator, DomainException, EmptyIterator, FilterIterator, InfiniteIterator, InvalidArgumentException, IteratorIterator, LengthException, LimitIterator, LogicException, NoRewindIterator, OutOfBoundsException, OutOfRangeException, OverflowException, ParentIterator, RangeException, RecursiveArrayIterator, RecursiveCachingIterator, RecursiveDirectoryIterator, RecursiveFilterIterator, RecursiveIteratorIterator, RecursiveRegexIterator, RegexIterator, RuntimeException, SimpleXMLIterator, SplFileInfo, SplFileObject, SplObjectStorage, SplTempFileObject, UnderflowException, UnexpectedValueException
> --------------------
>
>
> What should I look for?
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJLTMIFAAoJEF+XTK08PnB5easP/j/6BYQfUls5Lk7Hj6cDocAA
dErxSfMHjaWo8m8ijJbNTWw0Y44OnOE7JcK7UrrXHPABSuEpD6Bd5W0qbcc02Lg1
1XigYyqo4vlEOZ3J22lHhDPiNOGVybmxVR7NhqBbK5Jigy/Nddq6lnYjGbDF8PBO
uvHwVzueBdYtxMZIyfHw9NmLcnojEAtkgILS/DqAYZ8DaCcc/NJaR1tqrZhUAj5a
tOC40WaH7tNZupU2knQpxPPyy3QqQ9h/WK3BZhbpICiwNc2BW9Ky3m7fblO0vDYo
MwkBpsl64CAyNfwkiFDOpS9l0XStjTpKHNRJpQJNtZfH504EVOK+JlIWxBeNGtA0
BjY8Q2oaWhKiWaW607PUcY1+y/5Cq+1EGvB/BLkjFqjJfLiPDO5HfAnOy5OSgY9F
BB1Lx7DhzhWIX1WbPtsc5lacGnAQnT3rkDW7SUFNSTy5PrCurAvl8uMrLN2O5HIK
ytH4KAu1eqbZ6bqfQHI3L+4XvvOevadRisyLRjMa/KyQY4ZXgrHXjKdmVWvYypjo
nWuPefAetcT32CC3ua81x5uDUTG4cB3dBRoNd9QjMs0p5nkUnqkTQ0OEquRO3/Lm
8aaydzFNdKLinmlyEGH3F4VK0ITlQ2w1FOHdzzXX3aRIT+Gu7iRrb/IwqgHIKxKM
lk6/8y4zQltC0ZaLzMcA
=IogS
-----END PGP SIGNATURE-----

After good night’s sleep I figured it out myself.

For some reason :shame: I opened the html page directly from browser with “Open -> File”. That causes the php-script to show. When opened on localhost everything is ok.

Does this behaviour seem odd to you? I does, please someone try it also and report the bug to whomever it should be reported.

Not a bug but a feature. :wink:

PHP code relies on the interpreter in the webserver to run it. If you open the file directly you bypass the interpreter and none of the PHP code is executed.

Case closed.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Exactly… otherwise how would you modify the PHP code?

Good luck.

ken yap wrote:
> Not a bug but a feature. :wink:
>
> PHP code relies on the interpreter in the webserver to run it. If you
> open the file directly you bypass the interpreter and none of the PHP
> code is executed.
>
> Case closed.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJLTdkHAAoJEF+XTK08PnB57hAP/0ddteCugIiHkzRzalxNcy33
bLxr0Pw5B665+N5YK8oRL8dzAsnUi8lqrFeNLp9oGrYyxA6wXJVCVbEQlhk84nPF
LZ2OpRX3CYbkTEmX/4U6XsmfF4A3X1bASZeLGPAgArRrt21JSCem7bVBS6+Gc0HO
75eanm/c6nNlUe0WZfgUCfOQAiJUgqNXTtbor21zu874vDd5Mn1OzCjaEvkv21ip
xxGH7GXC6tOsoieKtyQrLqg4ndJUetZ4rRqLNVW57xO2Nf0oqqdKgnzyQv5FdfML
9g1K1472Of2Ub4Zqq1hjg2wEt/caKK69RcCp1JdTTWGLJDk2/zb8YxGXa1aHbvHj
wVLnd2HsETkWevEQFGy9ZPWsUO4FL1TXImu0Aq3OUzfwnnGdFvqco0O9zb1cwefX
7TBcfRJjS7QuplzzT/OmUOIR3/cCG8Np+JKn5vgdRte3cc23fQ/gjgNxsVyrbea2
3DVxR8kER91CCC4GBh0xfmh2nssEmyVGHkWu96/1+cym8l3FqFyInki31OCTLsT1
ZFg1UuORtoIt5cZyLdRMihoLk5MQ0CA6QFAyOYYZ9Gjl0NfBZ3tgOjc2f45uZFoK
6004Dl4Ll0KFPa4NFkIonoXKpj58k7lFT32hdmzolvIn+XyU0NQquxtICj6Ag7mw
8jrrbDPvIKQxVCOByyCw
=7U7h
-----END PGP SIGNATURE-----

I use editor to modify it - not browser. :wink:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Always good to use the right tool for the right job. :wink:

Good luck.

kansalliskala wrote:
> ab@novell.com;2103659 Wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Exactly… otherwise how would you modify the PHP code?
>>
>> -
>
> I use editor to modify it - not browser. :wink:
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJLTx4BAAoJEF+XTK08PnB5TVwP/Az0nAH2IIe/vpVrhepNhbbf
Wc0c7euY0QbDvqRuE7oSaAHsnwtGK0/oRysgTtF4oz4TFGhGjXz4JAv9FVMTEpTP
7PSmb/vZPq0ZnnbRl/UuwrCEQVn2lobekpPmusiQG47KZpuqGoPtwyGA0ePLqD6l
Uuek0thv/AddTo1axOFVlwNYLQuDS2XHRbqzqf/mwjuAVU02u3oEwaKOXMvJyRRV
tCNzWgi4LdjUcliap5l806wVjiFb6dbUT2RM9I/nXMGcNl6KPlhBDoELl6exnsBW
2191AkBP/90C44IEzYvn0hZDmRsYXTHCiDovEBI/7PgAMETryJ6I0WyBJ58wlYX8
1Yuh4nATveX1Y+TkkfjMUqGAXB5hheHQ8XbnRq4K2zBCNxmwSstG224RLIrdWXDK
EsHDj548lctxfy99usqr394UN+LpYietYuTbLdy1BS8rUE2AUOpzh8DosBt6O00+
zCuujgRJ4uhiSACHegVP9ND+hoy2h/e4rHlam9JAnMpXwJw4rWgoLmKGz9OxUiOj
zKKx8GS4eSoHYaPhoyoB6TI/3u8jUE8AhbaZtHG66N/Utn40j38lV1yLMDP07lV1
okNFpk72vvZDU2f9leXla86DPti39tJfwJB8gVk2l5lGqyBcymAIFnxbMA8jg89+
eSgDCVshRzktO0uAiImJ
=baGG
-----END PGP SIGNATURE-----