I'm exploring several types of programming style in web designing. But it suddenly came to my mind. Can a PHP file be read using JQuery/JavaScript on a HTML file. An example. I would open login.php using $.ajax inside the index.html page. Take note about the extensions in the example
-
Strange question... The answer is YES. From what I understand out of it. Using jQuery from a HTML page, you can do an Ajax request to a PHP file. Is that really the whole question? – Louys Patrice Bessette May 05 '17 at 02:09
-
If you mean read the code of that file, no. You only can make a request to login.php via AJAX and get the response. – Gerardo May 05 '17 at 02:13
-
Yes, it is strange but somebody told me that using PHP on loadup of the page is bad for reasons I forgotten. Mind enlighten the pros and cons of these two/three languages on loading up a page? – Calvin May 05 '17 at 02:14
-
1Ha.. I see. PHP runs on server-side. «on loadup a page» can only be related to client-side. Where the "produced HTML and scripts" (produced by PHP) are served to the client browser. Please read this (and search for similar topic) : http://www.seguetech.com/client-server-side-code/ – Louys Patrice Bessette May 05 '17 at 02:16
2 Answers
Calvin!, your question really is unclear!
And is denoting very few efforts...
Based on the reading of all comments, I can answer this with examples:
In a test.html
file:
<span>TEST</span><br>
<?php
echo "PHP works.";
?>
outputs:
TEST
But the exact same code in a test.php
file outputs:
TEST
PHP works.
NOW using jQuery in an
test2.html
file to access a separate PHP file asynchronously.
Assuming this basic ajax-requested-file.php
which content is:
<span>Ajax content!</span>
If you call it from a test2.html
file like this:
<span>TEST#2 using Ajax</span><br>
<div id="ajaxResult"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script>
$.ajax({
url:"ajax-requested-file.php",
data:"",
method:"post",
success:function(html){
$("#ajaxResult").html(html);
}
});
</script>
It outputs:
TEST#2 using Ajax
Ajax content!
Note that, if you are really attentive...
You will notice a milliseconds delay between the appearance of the first line and the second one.
Like here : https://www.bessetteweb.com/SO/43795339/test2.html

- 33,375
- 6
- 36
- 64
-
In regards to my English explanation... I got some problems on that since it is not my native language. But thanks for the explanation. It answers my question. – Calvin May 06 '17 at 12:44
Technically you can send a PHP file to a client, but a browser cannot execute PHP code, so there is no point in serving a php script to the client side.
If you are looking for the right web site architecture you should look into the single page architectural style. With it you just create a single HTML page for your site and load any additional content via ajax requests. For changing the page content you rely on js frameworks that manipulate the html DOM tree dynamically in place.
Note that you don't have to be strict on the single page. You can apply the same style for say a handful of logically different pages in your application as well.
To read more see this article and this answer.

- 1
- 1

- 9,166
- 1
- 33
- 46
-
Single page application versus multiple pages is not the question asked by OP. Even if you provide interesting references... – Louys Patrice Bessette May 05 '17 at 02:23
-
Added the info about PHP file, but the OP is also mentioning that he is looking into which programming style to use and thus the answer should be useful. – Oswin Noetzelmann May 05 '17 at 02:25
-
Okay.. Agree. I re-read the question, and your edited answer make sense. The thing is that this question is basically unclear... So I un-downvoted. ;) – Louys Patrice Bessette May 05 '17 at 02:26
-
Sorry but I'm quite a bit beginner in the programming industry so the stuffs you said is quite advance for me and I get it that a html file (.html extension file) cannot run codes/scripts in it, but I really want a Yes or No Answer in the question. "Can I open and use a php file/code/script inside a HTML file/page via JQuery?" – Calvin May 05 '17 at 10:20
-