Can't use php variables defined in another file - javascript

I have three php files, one defines a constant, the other has the html markup and includes a php file wigh renders js content. the third is the php file rendering the js content. the code is as shown below.
the first file named "config.php" :
<?php
define('TESTVAR','test variable');
?>
the second file named "main.php" :
<?php
require_once ('config.php');
?>
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script type="text/javascript" src="script.php"></script>
</body>
</html>
the third file named "script.php" :
alert(<?php echo TESTVAR ?>);
this is what gets loaded as script.php:
<b>Notice</b>: Use of undefined constant TESTVAR - assumed 'TESTVAR' in <b>C:\xampp\htdocs\trials\testphpinjs\script.php</b> on line <b>2</b><br />
TESTVAR);
it's not that php is not being rendered, if i change the script.php to
alert(<?php echo '\'testing'. 'php' . 'echo\'' ?>);
it works perfectly and the loaded script can be viewed from developer tools as
alert('testingphpecho');
the only problem I have is that I cannot access the variables in pages required or included before

Using <script type="text/javascript" src="script.php"></script>, the browser will issue a new request to load script.php directly. Since PHP is stateless, that request will not be handled in the same context as whatever processing goes on in main.php.
If you want to use the variables/constants defined in config.php within script.php, you will have to load it in there:
<?php require_once('config.php'); ?>
alert("<?php echo TESTVAR; ?>");
Note that I've added quotation marks, since TEST_VAR is a string value in your example.
If your Javascript code relies on more than just config.php and its behavior depends on pre-processing that's done in main.php, it might be better to load it as an inline script instead:
<?php
require_once ('config.php');
?>
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script type="text/javascript">
<?php include('script.php'); ?>
</script>
</body>
</html>
This way, PHP is loading the script.php file within the same context as main.php, and all constants and variables defined there will (should) be available for use by the code that's outputting your Javascript.

Related

Include JavaScript files to the document head with php

I'm creating a blog where I included header.php(Header, included in every page). Header contains a section where all the scripts (Scripts that will need in every pages) will be included.
header.php
<!DOCTYPE html>
<html>
<head>
<!-- Scripts Section -->
<script src="example01.js"></script>
<script src="example02.js"></script>
<title>Demo</title>
</head>
<body>
<p>Blog</p>
But in a specific page, I've to include a specific script (file.js named).
index.php
<?php
include('header.php');
importjs('file.js'); /* Any Function To Import JavaScript*/
?>
If I include the JavaScript file here then it'll be included in body not the scripts section in the header. I don't know how to create that function. So please tell me how to create that function. create function in header.php and please first include the header before using function.
Thank You
I think, you should to write your line code like this example:
<?php int a; ?>
<script type="text/javascript" src="file.js" />
<?php int b; ?>

What should be use to share variables between pages in HTML and PHP?

I need to create a centralized page where all the variables that are needed in more than one page get set. For example:
centralized.php
$displayPath = "/var/www/html/wordpress/";
Then, I need html and php pages across the server to have access to it. For example:
page1.html
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
</body>
</html>
<?php
echo ($displayPath);
?>
page2.php
<?php
echo ($displayPath);
?>
I was looking at using window.localStorage, but when doing so, how could pass the value from JS variables to php variables.
If you only want to read the variables from different pages, you can create a file with all of your variables:
myVariables.php
<?php
$displayPath = "/var/www/html/wordpress/";
$myglobalvariable="hello";
?>
And then include it in every page that needs it.
page1.php
<?php
require_once("./myVariables.php");
?>
NOTE: This will not allow you to change the variables in one page and then access the modified variables in another page. If that's what you want, you can take a look at this:
How to access a variable across two files

how to convert javascript variable to php variable in the same file

I have file named test6.php having javascript variable i need to convert this variable to php variable but when i run it give me an e error (PHP Notice: Undefined index: variable in /Applications/MAMP/htdocs/test6.php on line 14)
<!doctype html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
var variableToSend = 'foo';
$.post('test6.php', {variable: variableToSend});
</script>
</head>
<body>
<?php
$x = $_POST['variable'];
echo $x;
?>
</body>
</html>
note that i have only one file (test6.php) containing javascript code and php code and i am trying to convert javascript variable to php variable in the same file and i need to use post not get or submit form
Why error
you should know the processing of a php file rendering from the server to the client , it is :
1. client request the page
2. PHP parser (in server) -> php 2 HTML -> send to client
2. web browser load HTML
3. JavaScript running ~
so you can't get variable of "variable" when you run your php file . Becuase Js has not been running ~ . you should print the value of php echo at JS after ajax .
how about change the JQuery code
$.post('test6.php', {variable: variableToSend});
to
$.post('test6.php', {variable: variableToSend} ,
function(returnValue){
console.log(returnValue) ;
});
I think it will be ok , but i am sorry i can't test it ~
Check to see if it is set before you output it
<?php
if( isset($_POST['variable']) ) {
$x = $_POST['variable'];
echo $x;
}
?>
But I do not think this is what you actually want. The Ajax call is not going to update the current view.
Bring the php section to the top and use isset($_POST['variable']) to check if the variable exist or not.
<?php
if( isset($_POST['variable']) ){
$x = $_POST['variable'];
echo $x;
return;
}
?>
<!doctype html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
var variableToSend = 'foo';
$.post('test6.php', {variable: variableToSend});
</script>
</head>
<body>
</body>
</html>

coffeescript inside php file

Is it possible to use coffeescript inside .php file, for example :
<!--test.php-->
<html>
<head>
<title> use coffeescript inside php file
</head>
<body>
<h1>how to use coffeescript</h1>
<p><?php echo $lorem ?></p>
<script type="text/javascript">
// i want to use coffeescript here
(function() {
alert ('hello world')
});
</script>
</body>
</html>
If I want to use coffeescript in my php file what should I do to make it work?
I see no reason why not.
https://github.com/alxlit/coffeescript-php
Load your coffee in a var, compile it, and stick the resulting JS variable between the <script> tags.

How Can JQuery be embedded in the <body> and </body> Tags in Joomla 3.1?

I've added this code below to my default.php file in joomla 3.1.
<?php
JHtml::_('jquery.framework');
JFactory::getDocument()->addScript(JURI::root().'template/mytemplate/js/jquery.min.js');
?>
This only embeds the script inside the head tags.
Is there a way i can make it appear in the body tag?
You can simply put
?>
<script type="text/javascript" src="<?php echo JURI::root();?>templates/<?php echo $app->getTemplate();?>/js/jquery.min.js" />
Into your template index.php
or
?>
<script type="text/javascript" src="<?php echo JURI::root();?>templates/mytemplate/js/jquery.min.js" />
into any template override
I can't see the benefit of doing this though at all.
As mentioned in my comment on the other question, you can't import jQuery in the <body> tag using the JHtml() or JFactory method. These methods add the script to the <head> tag automatically.
The only way you can achieve what you want is to use <script> tags, however please see how I have done it:
<?php
// load jQuery, if not loaded before
if(!JFactory::getApplication()->get('jquery')){
JFactory::getApplication()->set('jquery',true);
?>
<script>
jQuery.noConflict();
</script>
<script src="<?php echo JUri::root(). 'template/mytemplate/js/jquery.min.js'; ?>"></script>
<?php
}
?>
This basically checks to see if jQuery is being imported already and if not, then it imports it. Add this code to the index.php in your template folder.
If you see the following in your index.php:
JHtml::_('jquery.framework');
then remove it.
Please bare in mind that this is not a recommended method however it's the only way that I know of to import it after the <body> tag.
Also note that if you ever update your template, then your changes will be lost

Categories