index.php vs index.html [duplicate] - javascript

This question already has answers here:
when to use index.php instead of index.html
(10 answers)
Closed 3 years ago.
I am not understanding when my main website page should be index.php and not index.html and what's the difference between the two.
And in case if it's index.php, how to manage with the javascipt code.
Thank you.

my main website page should be index.php and not index.html
In a simple web server configuration, if you request a URL which maps onto a directory, then the server will look inside that for an index document.
It will do this by comparing the files in the directory to a list of possible index document names which often include index.html and index.php.
It checks them in the order of its list and stops when it finds a match.
Adjacent to this, such servers are typically configured to serve .html files as static files and to execute .php files as PHP and serve the result.
And in case if it's index.php how to manage with the javascipt code.
Generating HTML from PHP instead of a static file has absolutely no bearing on how client-side JavaScript is managed.
If you're talking about server-side JS, then you probably don't want to be using PHP at all.

index.extension is the main trigger file which can be .html, .php or other extensions.
in case of server end programming or you are using PHP language then trigger file should be index.php while for only html content you can go with index.html
Regarding, adding javascript in index.php file, it could be like
<?php
echo "I am here in PHP code";
?>
<script>
alert("I am here in javascript");
console.log("here in console");
</script>
<?php
echo "I am here ending code";
?>

Related

How to run php interpreter remotely?

I'm trying to run php interpreter remotely in a html file. Basically the html file contains the php code which I want to execute it by using remote php interpreter via reference or something. I did it with other languages and it is working.
Like:
For JAVASCRIPT: (we directly write "javascript")
<script type="text/javascript">
document.write("This is Javascript!")
</script>
For JQuery:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
For ANGULARjs:
(I'm not sure it might be...)
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js">
</script>
Is there way to include the link for php too,
like something:
<script src="php.ini">
or
<script src="<path to php webserver of remote>">
</script>
Can anyone please tell me how it is possible or the way it can be get it done.
If you are using a server and you wish to execute php in an html file, you will need to tell your server to execute .html files as .php with apache2 you need to add the below code snippet to .htaccess in the current working directory or a parent one, this will make all .html files get handeled as if they where a php script
AddType application/x-httpd-php .html
I'm not entirely sure why you don't just use .php file extenstion, but hey there are many valid reasons...
One thing to note, all of the languages you posted are javascript based, javascript is a client side language, whereas php runs on the server. There is a HUGE difference between how they work.

How to list all files in folder?

So I have this page I'm working on and I have a directory named 'products' in the same directory where index.html is. I'm trying to create a script that will count all the jpg files in the 'products' folder and then create an img element with each of those. any ideas of how i can do this?
This can only be done in a server environment or using something that simulates a server environment on your local computer, like MAMP/WAMP or Node. Javascript cannot access local folder contents in that manner. It can get and insert local content like images, videos, or Javascript/CSS files, but that's about the limit of what it can do.
This is for security reasons. Imagine opening someone else's Javascript file on your computer and it runs a script to erase all the files in your C drive.
If you want do this on server side then use a PHP and create your own code like this one:
// allimg.php
<?php
$dir = "path/products/";
$imgs = glob($dir."*.jpg");
foreach($imgs as $image)
{
echo '<img src="'.$image.'">'; // or <span>$image</span>
}
?>
And HTML index.html File to view it correctly.
<html>
<head>
<title>Get All Images</title>
</head>
<body>
<?php include 'allimg.php'; ?>
</body>
</html>
All modern browsers'll refuse you attempting show a local folders, files because of protection rules.

Is it okay to save my javascript in a .php format for a wordpress site?

Instead of putting my javascript into a .js file, I would like to use a .php file so that I can use some php code (for example getting my wordpress blog url, or "if this page" statements).
Are there any down sides in terms of performance if I do this?
You can have a .php page and have your js functions within it with the proper <script></script> tags, as you said, and there shouldn't be anything wrong with it.
Though, if you have lots of js functions that require its own page, then you can pass the values that come from the server-side to the function in the client side. For example.
in .php page
<script>
var blogUrl = "<?php echo get_bloginfo('url'); ?>";
myFunction (blogUrl);
</script>
and in .js page
function myFunction (blogUrl)
{
alert(blogUrl);
//do something
}
This way, you can have a proper pure js file, while not making your php files too messy with JavaScript either.
Yes.
You can echo or print your js like so
<?php
echo "<script type = \"text/javascript\">
// code here
</script>":
?>
Remember to escape any characters with a backslash.
OR
<?php
//some php here
?>
<script type = "text/javascript">
//code
</script>
You can have a php file request return javascript content, and have it act as if it were an external javascript file (without the need to write the HTML tag around it) by setting the Content-type header to a javascript mime type:
example.php:
<?php
header("Content-type: text/javascript");
?>
alert("<?=$_SERVER['HTTP_HOST']?>");
Then you can use this php file as if it were a regular external javascript file:
example.html
<script type="text/javascript" src="http://www.myserver.com/example.php"></script>
In this case looking at the source of the javascript that the php responds with would just contain:
alert("www.myserver.com");
As far as performance goes, you might have a small hit by processing a php page instead of just serving a static file, but if you add suitable cache layers in front of it it shouldn't make much difference.
Yes, you can easily write your javascript code in .php file without using any php tag.
This is totally same as you are writing javascript in HTML. Have to use tag, which you did not use in .js file.
Yes can code js inside a php file but, remember to add tags or else your Script will not function
Scripts are flexible as both external .js file and within a php file in spite of the CMS we use.
Example:
<?php
<script type = "text/javascript">
// Script code to be embedded
</script>
?>
Above is the standard Syntax to be followed for integrating Script inside a PHP file.

PHP Ajax jQuery cross domain file loading

So before you say this can't be done. These are all files that I have one server. I just have some of them listed under different domains.
My PHP script will access all the files but when I try to do an ajax request to try to load the file I will often get an error (because the site i am accessing is secure and the one I am accessing it through isn't).
What I need is a way to have php grab the file. But I need aJax to retrieve the file and render it for me. I am also using ACE editor to edit the file
The bit of code I have here will actually error out as well because it will load and print out the file where $page is defined but won't load where htmlspecialchars is.
<script>
var e = ace.edit("editor");
<?php
$page = readfile($_SERVER['DOCUMENT_ROOT'].$_GET['dir']);
echo 'e.setValue('.htmlspecialchars($page, ENT_QUOTES).');';
?>
</script>
I have an ajax get request working but it doesn't work when I go to a directory with a special htaccess file. Now I can't change the htaccess file (unless there is a way for me to confirm that it is my script running and not someone else.
The question is, how can I access those other files without getting that error? Mind you those files could be extension. It is not limited to just scripts or css, mostly they will be html or php files.
After an hour of searching the deep dark depths of the php.net site I was able to put together a solution that works.
<?php
echo htmlspecialchars(
addslashes(
file_get_contents(
$_SERVER['DOCUMENT_ROOT'].$_GET['d‌​ir']
)
)
); ?>
the addslashes is the extra part that I needed. Then I also had to put it between the div for the editor. I couldn't use the editor.setValue() function.

PHP code-igniter

I'm having troubles calling my external js files from my views php pages. The js files are saved in the "resources" folder in the root folder i.e "root/resources/scripts/myfile.js" just like the "root/application" and the "root/system" folder.
so in one of my php files in the views folder i am using this code:
<script type="text/javascript"
src="<?php echo base_url();?>resources/scripts/welcome.js">
and this php file is located in "root/application/views/welcome.php"
and still my page cannot link with the java script so i am stuck big time.
regards
k..
Try this:
<script src="<?= base_url('resources/scripts/myfile.js')?>"></script>
Looking at your question and the comments below your question, it seems you are using "resource" in one example and "resources" in another example.
Once fixed, view the source of the page (I use FireFox, as it makes the links clickable), search for "welcome.js". Once found, click on the link for that file. If you see javascript, it means the file is found, alternatively you will see a 404 error.
First of all check if the resource folder is available,
The java script file is available and the file naming is in the same small or capital case, and server has public permission of the file and folder.
by the way your code has missing end tag
</script>

Categories