pass parameters using variable? - javascript

This is a simple syntax question.
I declare a variable:
<script type="text/javascript">
var id_1= '<?php echo $id; ?>';
</script>
And then in a externally loaded js file im trying to call a function using the variable (the external js file is loaded after the ^^ variable declaration:
loadComments(id_1);
The id_1 is being passed literally as 'id_1', not recognizing it should be a variable. What am I doing wrong?

var id_1 = '<?php echo $id; ?>';
Will echo something like this:
var id_1 = '10';
Which is treated as a string in JavaScript. You want to do this instead, so that you assign a number to id_1:
var id_1 = <?php echo $id; ?>;
This will print out something like this:
var id_1 = 10;

The php code is recognized only by a .php file.
Put your code in a .php file and run it on you local server

Related

How to use base url or php variable in javascript file

I am new in PHP, i want to use "base_url"(php variable) in javascript file,But right now i am unable to load image,Here is my current code,How can i do this ? Thanks in advance.
$('#site-logo').find('img').attr( {src:'<?php echo base_url(); ?>assets/images/logo/logo_dark#2x.png',width:'151',height:'45'} );
You can create a variable or a constant in the javascript and store the base url there and use the same in the code.
Something along the line of following
var BASE_URL = "<?php echo base_url(); ?>";
$('#site-logo').find('img').attr({src: BASE_URL+'/assets/images/logo/logo_dark#2x.png',width:'151',height:'45'});

How to pass a php variable to javascript that is being executed by php

I have some javascript code that is executed by php. The reason for this is I tried to print to a printer but I couldn't get php code to do it.
But the javascript code can. And I need to post a variable to the javascript so it knows what to print. Therefore I know I can post to php and from there echo the javascript to connect and print to my label printer. The last hurdle is passing in the variable received by POST to the javascript.
$val = "variable";
echo '<script>
var val = "<?php echo $val; ?>"; \\ trying to put the variable into the javascript
var format_start = "^XA^LL200^FO80,50^A0N36,36^FD";
var format_end = "^FS^XZ";
BrowserPrint.getDefaultDevice(\'printer\', function(printer) {
default_printer = printer
...
While I agree with the comenters about not doing this, there is a simple misunderstanding in your code:
You are running PHP to output JS from the echo, you do not need to output the PHP tags, you can just output the variable, and when the page renders, the variable substitution will occur.
So, this:
var val = "<?php echo $val; ?>";
Should be
var val = "$val";
The JS at page render, visible via view source, will now look like:
var val = "myValVarContents";
I hope that helps, even though as said, this is generally a bad idea
D

How to convert file content to String in php

click here to see the errorI'm trying to convert a local php file into string to store it in a Javascript variable.
Is there any way to acheive this?
Here is my php code.
<?php
$data = get_file_contents("htmlFile.html");
echo $data;
>?
This data needs to be stored as string in Javascript variable.
I'm facing the below issue:
enter image description here
Please give your suggestions to achieve this.
Try this:
var data = "<? echo $data; ?>"
Remember using "" characters. That is what is generating erros.
Try This:
<?php
$data = get_file_contents("htmlFile.html");
?>
and use this script
<script>
var jsvar = "<?php echo $data; ?>"
</script>

How to use defined variable(defined in php file) in .js file

I have a config.php file in which I defined a variable SITE(define('SITE',"siteroot") and I have a .js file which included jquery functions. In that file, I have a variable site_root.
Is it possible I can make site_root=<?=SITE?>
I change the .js file to php file and used this code header("Content-type: application/javascript"); to defined the type of the file and include the config file. However, site_root=<?=SITE? return "".
I'm not sure if you have a typo in the question or in the actual code, but
define('SITE',"siteroot");
and
var site_root = "<?= SITE ?>";
// ^ ^ quotes matter
should work fine.
Declare PHP Variable
<?php
$variable = "abc";
?>
<script>
$("document").ready(function(){
var test = "<?php echo $variable; ?>";
alert(test);
});
</script>

Set Javascript Variable From PHP Get Variable

I have a page which I have collected two variables, I want to pass these variables to my document ready function. but for some reason.. My alerts for testing are just outputting the line code rather than the result.
just to note the JS script is in another file which is linked to the page.
PHP
<?php
$cardid = test;
$custid = test;
?>
Javascript
$(document).ready(function(){
var custid = '<?php echo($custid); ?>';
var cardid = '<?php echo($cardid); ?>';
alert(custid);
alert(cardid);
});
Put your javascript code into a separate php file under script tag:
<script>
$(document).ready(function(){
var custid = '<?php echo($custid); ?>';
var cardid = '<?php echo($cardid); ?>';
alert(custid);
alert(cardid);
})
</script>
Include this file in your main php file (using the include function).
It is not possible to execute php in a separate .js file.
you aren't assigning anything to your PHP variables:
$cardid = test;
$custid = test;
a value which isn't numeric and isn't in between quotation marks is considered a constant in PHP. and the constant test most likely has no value in your PHP environment.
you wanted to assign a string, i guess, so don't forget the quotation marks:
$cardid = "test";
$custid = "test";
Wrong concept.
Your JS is not parsed by PHP, so your approach will not work.
Lets make it work:
You have to give the Variables to JS with one trick: Write it into the HTML in a script-tag, then you can access them in your JS.
Lets assume, you produce a HTML like this in your PHP:
<body>
<div>Hello World</div>
</body>
Now, write the variables for you JS into it, here is a simple approach:
echo "<script>var myVar1='".$_GET['var1']."';</script>\n";
echo "<body>\n";
echo " <div>Hello World</div>\n";
echo "</body>\n";
Okay, very simple, do not do it like this in real, but you got the concept?
So, that was the trick to do this.
P.S. The same way, you get $_POST Data to your JS ;)
Consider using quote to represent string.
<?php
$cardid = 'test';
$custid = 'test';
?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.js"></script>
<script>
$(document).ready(function(){
var custid = '<?php echo($custid); ?>';
var cardid = '<?php echo($cardid); ?>';
alert(custid);
alert(cardid);
});
</script>
PHP code will only be run in files ending with .php. Your.js file is loaded directly and not subject to PHP processing. If you wish to assign values from PHP variables to javascript variables you will need to do so in a script block in your .php file.
For example if you add this to your .php file -
<script type="javascript">
var custid = '<?php echo $custid; ?>';
var cardid = '<?php echo $cardid; ?>';
</script>
Then in your .js file shold look like -
$(document).ready(function(){
alert(custid);
alert(cardid);
});
It is of course possible to run PHP in a .js file. As in the case of running PHP as an Apache module, you can add whatever extensions you want Apache to recognize as PHP scripts, such as:
AddType application/x-httpd-php .php .html .htm .js
starting with php5.3 you could probably get issues so you can try also:
AddHandler application/x-httpd-php7 .php .html .htm .js
AddHandler application/x-httpd-php .php .html .htm .js
AddHandler x-httpd-php .php .html .htm .js
if you still get issues use a 301 redirect or a rewrite rule in your .htaccess, like this for example:
Redirect 301 "^(.*)/dir/example.js$" "http://www.domain.de/dir/example.php" [NC,L,QSA]
if you are on lighttpd go to /etc/lighttpd.conf or on Windoze C:\lighttpd\etc\lighttpd.conf, uncomment the "mod_cgi" line and add this line:
cgi.assign = ( ".js" => "c:/php/php-cgi.exe" )
don't give up just because someone say to you "you can't do this or that" ;-)

Categories

Resources