dakke Posted October 24, 2010 Share Posted October 24, 2010 This is most likely a noob question, but... why does it not work? <html> <head> </head> <body style="background-color:#fff;margin:0"> <div style="border-top:1px solid #404040"> 1. JS: source file <br /> <script type="text/javascript" src="test2.js"/> </script> <br />----<br /> 2. PHP: source file<br /> <script language="php" src="test.php"/> </script> <br />----<br /> 3. JS: script inside<br /> <script type="text/javascript"> document.write('<b></br></br>Hello World</br></b>'); </script> <br />----<br /> 4. PHP: script inside <br /> <script language="php"> echo("Hello World"); print "hello world"; </script> <br />----<br /> </div></div></body></html> with the test.php file in same dir (using MAMP), the test.php does work when loading the file directly: <?php echo("Hello World"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/216719-include-php-file-in-html-why-does-it-not-work/ Share on other sites More sharing options...
mentalist Posted October 24, 2010 Share Posted October 24, 2010 PHP only executes on the server, not within the browser. <html><head></head><body> 2. PHP: source file<br /> <?php include "test.php"; ?> <br />----<br /> 4. PHP: script inside <br /> <?php echo "Hello World"; print "hello world"; ?> <br />----<br /> </body></html> Both of these would get executed before being sent back to the client, therefore there should be no php code within the page served to the client. Quote Link to comment https://forums.phpfreaks.com/topic/216719-include-php-file-in-html-why-does-it-not-work/#findComment-1125912 Share on other sites More sharing options...
dakke Posted October 24, 2010 Author Share Posted October 24, 2010 Agreed, but when loaded on a MAMP install, that should work right? If you execute a PHP script on a local server install then that should be executed, or am I not understanding it correctly? If the file 'test.php' does print, then why doesn't it when included in the html? Quote Link to comment https://forums.phpfreaks.com/topic/216719-include-php-file-in-html-why-does-it-not-work/#findComment-1125915 Share on other sites More sharing options...
mentalist Posted October 24, 2010 Share Posted October 24, 2010 Only ever just hearing of MAMP, I see that is basically same as LAMP, or same as installing a web server with PHP and a db (MYSQL). So, no... The web server is standalone (includes the PHP interpreter though), the MYSQL server is also stand alone. You access the web server (as you would on the internet, but just on a local loopback device), therefore you need a web browser (client). The server when required will make connects to the db. ALL PHP is executed in the server... No browsers ever execute PHP code!!! Quote Link to comment https://forums.phpfreaks.com/topic/216719-include-php-file-in-html-why-does-it-not-work/#findComment-1125917 Share on other sites More sharing options...
dakke Posted October 24, 2010 Author Share Posted October 24, 2010 If that is the case, then one would not be able to run a drupal (php/mysql) site locally! If the MAMP/LAMP/WAMP is NOT a local server, then it would not be able to run any PHP/MySQL site. Which it does. A MAMP is indeed a local server, so it should execute a PHP. I'm not talking about the browser executing PHP, I'm wondering where the code is incorrect. This is not a server-not-executing problem, but an issue concerning the code. Just don't know where. Quote Link to comment https://forums.phpfreaks.com/topic/216719-include-php-file-in-html-why-does-it-not-work/#findComment-1125919 Share on other sites More sharing options...
Pikachu2000 Posted October 24, 2010 Share Posted October 24, 2010 You seem to be confused as to the difference between something being physically remote or logically, or both. Quote Link to comment https://forums.phpfreaks.com/topic/216719-include-php-file-in-html-why-does-it-not-work/#findComment-1125925 Share on other sites More sharing options...
dakke Posted October 24, 2010 Author Share Posted October 24, 2010 Ok, I indeed don't understand that one. Regardless, a MAMP is a local server, and it can execute PHP code. Which is what I understood that the previous user said was wrong. So where am I minsunderstanding this one? Quote Link to comment https://forums.phpfreaks.com/topic/216719-include-php-file-in-html-why-does-it-not-work/#findComment-1125926 Share on other sites More sharing options...
mentalist Posted October 24, 2010 Share Posted October 24, 2010 Yeah, a local server runs locally, but it's not the web browser (the browser displays the html, js, css, xml, etc, BUT NOT PHP!!!) Here's an image to help you understand... http://www.learnphp-tutorial.com/ Yes, I understand what's confusing you... in the source file there is PHP code, but by the time the server sends it out to the client it has been executed and replaced with the output... The client (web browser) then displays what it understands (html, js, xml, css, etc) In your original post, your trying to execute php with script tags (html, js, css, type script tags...), those type of tags are client side... Try this file, call it "test.php" <?php print "hello world!<br />"; ?> Then find it via your server (e.g. DON'T just right click on the file and open with browser), for instance you may have to put 'localhost' in the address bar, then etc... maybe 'localhost/test.php' if it's in the root directory. Once displaying in your browser, press 'ctrl+U' to look at the source, see how the php code isn't there, but look at what is... p.s. don't even dare question my use of CTRL... lol p.p.s. what I said wasn't wrong! lol, one day you'll look back... Quote Link to comment https://forums.phpfreaks.com/topic/216719-include-php-file-in-html-why-does-it-not-work/#findComment-1125928 Share on other sites More sharing options...
Pikachu2000 Posted October 24, 2010 Share Posted October 24, 2010 Or even more simply: WEB BROWSER <---> WEB SERVER WEB BROWSER <---> INTERNET <---> WEB SERVER Whether the internet exists between the web browser and web server makes no difference. In both cases the web server is logically remote from the web browser. In the second case, it is also physically remote. Quote Link to comment https://forums.phpfreaks.com/topic/216719-include-php-file-in-html-why-does-it-not-work/#findComment-1125932 Share on other sites More sharing options...
tastro Posted October 24, 2010 Share Posted October 24, 2010 did you try this: <?php echo("Hello World"); print "hello world"; ?> instead of this? <script language="php"> echo("Hello World"); print "hello world"; </script> because i have never seen it before that php is called with <script language="php"> Quote Link to comment https://forums.phpfreaks.com/topic/216719-include-php-file-in-html-why-does-it-not-work/#findComment-1125938 Share on other sites More sharing options...
Pikachu2000 Posted October 24, 2010 Share Posted October 24, 2010 Is the file in your first post saved with an extension other than .php, by chance? Quote Link to comment https://forums.phpfreaks.com/topic/216719-include-php-file-in-html-why-does-it-not-work/#findComment-1125940 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.