How to write js file using php? - php

How can I edit a javascript file using php. I want to change the file path dynamiacally is it possible to do with php. Please help me?
test.js is my file. this is for display images on frontend
from admin panel me create one folder named flower. then I will create one flower named folder
and copy this test.js to that folder. after that I have to edit the file path of test.js file. how can I do that?

Your PHP file will get executed on the server and an HTML file (usually) will be sent to the client. This file will contain a <script type="text/javascript">... line somewhere if you're including a javascript file. You can rewrite this line in your PHP file to make it pick up the file from somewhere else.

Yes, it's possible, if you mean changing the <script src="..."> in your HTML.
Anyway, it's like
<script src=<?php echo '"' . $path . '"'?>></script>

Related

relative path not working in html attribute in .php file

I tried to use a URL in the parent directory of the root but it's not working in the HTML attribute of the PHP file.
<?php
include ("../func.php"); // working, will include the file in the parent directory
?>
<img src='../testimg.png'/> <!-- It only looks inside the current directory-->
<img src='..../../testimg.png'/> <!-- If I mess around with the relative path, it still looks inside the current directory-->
However, if I delete the PHP code and change the file extension to .html, the relative path in img src works normally.
Why is this happening and how could I solve the problem? I am wondering is there any PHP configuration file I need to adjust?
I have been stuck with this problem for a long time and googled a lot but didn't find a working solution. I would be appreciated for any help.
I tried your code in my local with the following directory structure
root_folder
|---my_folder
|__index.php -> (this file contained your code above)
|---func.php
|---testimg.png
<img src='../testimg.png'/> showed my image perfectly.

how to link html folder's file with php folder's file

I have 2 folders html and php, in html all html files exist and in php all php file exist.
In html folder-> buycars.html exist which link with viewh.php(this is in php folder) how to create a linkā€¦ I use
<a href="php/viewh.php">
that is not working.
you need to jump one directory back
<a href="../php/viewh.php">
You need to go up one directory. To do this you simply append ../ to your directory path;
Click
You can also go up multiple by stacking them like so;
Click
EDIT: #Sourabh beat me to it.

how to link a jquery file in global.config.php?

Actually i am writting a php script to link some javascript files in my multiple sites.
Example : site_dir1/js/jquery.1.4.2.js and
site_dir2/js/jquery.1.4.2.js
In this case, in the document_root location i have a file called "jquery/jquery.inc.php which has the follwing code
$jq_142 = "<script type='text/javascript' language='JavaScript' src='jquery-1.4.2.min.js'></script>";
I will use this code to make require my php file anywhere i want
<?php require $_SERVER['DOCUMENT_ROOT'].'/jquery/jquery.inc.php' ?>
And the reputation of jquery file keeps only one copy in my following location.
$_SERVER['DOCUMENT_ROOT'].'/jquery'
My doubt is still my javascript files are not loaded where ever i call the files.
I am little messed. how to fix it? any better solution?
If you're going to do this you need to give the script an absolute path rather than a relative one.
When you include it in index.php, the browser will look at /jquery.js for the file. When you include it in /folder/page.php, the browser will look check /folder/jquery.js.
See how it changes depending on where you are in the website? You need to tell it to look at a specific place, like:
$jq_142 = "<script type='text/javascript' language='JavaScript' src='http://example.com/scripts/jquery-1.4.2.min.js'></script>";
This means that no matter where you are in your site, the browser will check http://example.com/scripts/jquery-1.4.2.min.js for jQuery.
Finally, I found my page.php script have loaded my jquery.inc.php file. But the jquery file linking script has been coded inside quotes as follow :
$jq_142 = "<script type='text/javascript' language='JavaScript' src='jquery-1.4.2.min.js'></script>";
So it's couldn't be displayed. And i found this jquery.inc.php is not called by .html files. Only for .php files. I couldn't find any alternative solution for this problem.
I have linked by using the absolute path of my unreplicated jquery library files from jquery/jquery-xxx.js directly.

included files php do not appear in a browser

I have a php page(index.php) that includes another html file(footer.html). Am using dreamweaver, and my problem is: The included file(footer.html) shows in the main file (index.php) in dreamweaver design view, but does not show in the browser preview. I have both the php files in the root directory, so they're at the same level. I have tried using require rather than include with the same result. My include code is:
<?php include("footer.html");?>
does anyone see something wrong off the bat?
Do realy exist file "footer.html"?
If no create them.
Is file "footer.html" in the same directory as "index.php"?
If no add it to the same directory.
Do you have any content in your "footer.html" file?
If no add any and check if it appears on page.

PHP Includes function, not displaying images

This is all that is contained on my home.php page
<?php
include ('../bgs/bg-verbier.html');
include('../menus/menu-verbier.html');
?>
both of the requested pages lie in the parent directory. The /menus/menu-verbier.html has posed no problem from the include function, however the '../bgs/bg-verbier.html is. The file as been found however the images aren't displaying.
The path of the images is working on a .php page that lies in the same parent directory,
Here's the ../bgs/bg-verbier.html' file:
<div id="bg">
<b><i><i><img src="images/verbier/0.jpg"></i></i></b>
<!--b:holder, i#1: large canvas, i#2: center verticaly -->
<b><i><i><img src="images/verbier/1.jpg"></i></i></b>
<b><i><i><img src="images/verbier/2.jpg"></i></i></b>
<b><i><i><img src="images/verbier/3.jpg"></i></i></b>
</div>
<script type="text/javascript" src="js/scaler.js"></script>
<script type="text/javascript" src="js/example.js"></script>
Your images folder must be in the same directory as the php file that is calling it. Otherwise, you must specify the relative path to the images folder.
When you include a file, it's like if you were to copy & paste that file's contents into the PHP file.
So, it's like the PHP file is asking for images/verbier/0.jpg, which means it's looking in the same folder as the PHP file.
I suggest you edit the HTML file so it uses absolute paths to the images, like:
<img src="/menus/images/verbier/0.jpg">

Categories