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.
Related
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";
?>
Using Wordpress, I have the file search-form.php in my child theme's folder.
How do I insert this during a JavaScript routine from a file 404.php in the same folder?
I've tried using: <script type="text/javascript" src="search-form.php"></script> but this does not output the contents of search-form.php at the place I am calling it from.
Help appreciated.
You could use php includes:
<?php include "searchform.php";?>
This runs on server side, wich is good for loading speed.
Or you could use iframes:
<iframe src="searchform.php">Old browser!!</iframe>
Or you could dynamically load it with js/ajax:
<div id="show">loading...</div>
<script>
body.onload=function(){
//see ajax tutorials
document.getElementById("show").innerHTML=ajaxdata;
}
</script>
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.
I'm curious about passing PHP output to Javascript. Two things specifically:
I'm assuming this can be done safely using script tags on a .php page. Is there any reason not to?
Can you make PHP run on .js pages? What configuration changes would be required? And again, would there be a reason not to?
I'm assuming this can be done safely using script tags on a .php page. Is there any reason not to?
Yes. No reason not to.
Can you make PHP run on .js pages? What configuration changes would be required? And again, would there be a reason not to?
Yes, you would have to configure your webserver to use the PHP module for the .js suffix. On nginx you might add a location line for files ending in .js:-
location ~ \.js$
{
}
On Apache you'd do something like:-
AddType application/x-httpd-php .php .js
One reason you might not want to do this, is that any static JS files would go through the PHP module and that would cause additional overhead. Another option would be to reference the PHP file in the HTML script tags eg:-
<script src="myfile.php"></script>
and then make sure your PHP returns its output with the correct content-type eg:-
header('Content-Type: application/javascript');
You're right on both counts.
You can output dynamic content (PHP) in a <script> tag on a .php page
You can output dynamic content (PHP) in an all-javascript file, though the extension would still be .php
Example 1
<script type="text/javascript">
var myobject = {
data: '<?php echo $myPHPvariable; ?>';
};
// or better yet:
var myBetterObject = <?php echo json_encode($myPhpObject); ?>;
function foo () { ... }
</script>
Example 2
Not recommended
<?php
// This is an all-javascript file but the extension ends with .php
header('Content-Type: application/javascript');
?>
var myobject = {
data: '<?php echo $myPHPvariable; ?>';
};
function foo () { ... }
Then you include that file in your HTML document with:
<script type="text/javascript" src="/custom_javascript.php"></script>
It should definitely end with .php, and thankfully the web browser knows it is JavaScript by the type="text/javascript" part and because your PHP script outputs the right Content-Type header.
The first solution is simpler : declare some JS variables in the output HTML, they can be constant or generated by the PHP code:
<script>
var my_variable = '<?php echo($my_variable); ?>';
</script>
Then you can use this variable in your Javascript code.
The .js files should be static to allow the browser to cache them, so they won't be loaded every time the user loads a page. By generating .js files on the fly with PHP, you may have problems with browsers using cached .js instead of the expected generated file. For example, if your variable contains dynamic content as the last news from your website, a datetime, etc.
So, yes you can do it, but you'll have to prevent the browser to cache the file, increasing the bandwidth usage or expect unexpected behaviours with browsers loading cached .js files. There's no benefit in using this way.
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>