How to call a php variable in .js file? [duplicate] - javascript

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 years ago.
I want to use a php variable in .js file. Is this possible that we can call a php variable in .js file.
what I have done for this problem
I have .php file where i created var
$domainname = "wwww.google.com ";
In second file .php
<?php header("Content-type: application/javascript"); ?>
<script>
var DomainName = <?php echo $GLOBALS['SettingDomainName'];?>
</script>
and finally I want to call this var in js
<script src="public_https/test.js"></script>
alert(DomainName);
I got this message :
SyntaxError: syntax error
<script src="public_https/test.js"></script>

Use json_encode function which returns JSON representation of a variable which can be used inside JavaScript as-is:
<?php
$domainname = "www.google.com";
?>
<script>
var DomainName = <?php echo json_encode($domainname); ?>;
</script>
Note: no quotes. This function works for all types of variable: string, integer, float, boolean, null and etc.

<script>
var DomainName = "<?php echo $GLOBALS['SettingDomainName'];?>";
</script>

What about we hide your variable in a hidden box using php
$domainname = "wwww.google.com ";
<input type="hidden" value=" <?php echo "$domainname"; ?>" id="mydomain">
Then we fetch it using js or jquery
<script>
var domain=$('#mydomain').val();
</script>

<input type="hidden" id="domain_name" value="<?php echo $GLOBALS['SettingDomainName']; ?>">
JsFile:
var DomainName= document.getElementById('domain_name');

Related

PHP -file_exists() Function? [duplicate]

How I can set the JavaScript variable strUser from PHP?
I am using the following code:
<script>
function val()
{
var e = document.getElementById("ali");
var strUser = e.options[e.selectedIndex].text;
}
</script>
brand<select id="ali" onChange="val()">
<?php
$brand=modsearchkhodroHelper::retrieve();
foreach($brand as $item)
{
?>
<option value="<?php echo $item['brand']?>" selected="<?php $id=$item['brand']?>">
<?php echo $item['brand']?>
</option>
<?php
}
echo "</select>";
?>
If you want to set the variable when the page loads, you could use something like this in the PHP code:
<script type="text/javascript">var strUser = <?php echo json_encode($someVariable); ?>;</script>
Just make sure to remove the later variable declaration from the JavaScript.
If you want to set the variable after the page loads, you'll have to use an AJAX call to ge the value from the server.
Use Cookie
in your javascript
<script type="text/javascript">
document.cookie = "cookieName=cookieValue";
</script>
in your php
<?php
$phpVar = $_COOKIE['cookieName'];
echo $phpVar;
?>

php in external javascript file

I have a the code below in my js which obviously works if the js is in the php file
filter: '<?php echo $my_cat>'
how can i make a variable maybe in the php file and pull it in my external js file?
You have to place this below code at the top part of your test.php page
<script>
var filter = '<?php echo $my_cat>';
</script>
and simply you can check it on your external.js file by alert
alert(filter);
<?php
function showme(){
echo "HelloWorld";
}
?>
<script type="text/javascript">
var my_var = '<?php echo showme(); ?>' ;
</script>
You can also use short tags.
<script>
var filter = '<?=$my_cat?>';
</script>

How pass variable php in javascript in function?

so i want try pass variable php to function javascipt where variables is
<?php $code1=""; ?>
and for function is
<script type="text/javascript">
var $code1 = $(this);
$txt=new Array();
var $code1 = <?php echo $code1("code1"); ?>;
$(function(){
$('#go').on('click',function(){
console.log($('form').serialize());
})
$('body').on('keydown','.last',function(){
$('.last').removeClass('last');
$('#go','body').before(
'<table><tr><td><input class="last" type="text" id="code1" name="code'+(Number($(this).attr('name').match(/[0-9]+/g))+1)+'" value=" SHOULD BE HERE "></td></tr></table>');
})
})
</script>
should be value is ="$code1" so i try value= "<?php echo $code1; ?>" but it's cant can help me pls?
json_encode() is what you want, but all the quotes and what not are handled for you by the JSON-encoding. Don't add more quotes. Also, it's easiest if in your JavaScript, you assign stuff to their own variables.
var data = <?php echo json_encode($data); ?>;
Also, I recommend that you don't simply concatenate data into HTML like this. Otherwise, you're lacking escaping for HTML which can cause ambiguity and injection problems.
$('<input>').val(data);

Insert Variable PHP in Jquery [duplicate]

This question already has answers here:
Use a PHP variable in JQuery
(4 answers)
Closed 8 years ago.
can you help me
i have problem insert variable php into jquery, this my script:
<script type="text/javascript">
$("#add").click(function() {
var error = <?php echo $reportErr; ?>;
$("#table").append("<tr><td> <input type='text' name='report[]' /> <a href='javascript:void(0);' class='rem'>-</a>"+error+"</td></tr>");
});
$("#table").on('click','.rem',function() {
$(this).parent().parent().remove();
});
<?php
$report = "error";
$reportErr = $report;
?>
At first glance, you have a syntax error. Your string needs to be in quotes.
Try var error = "<?php echo $reportErr; ?>";

Passing php var with JavaScript [duplicate]

This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 8 years ago.
I'm trying to pass a php var that is a string using Javascript, but in the final result the string gets cmmented in html. Here's my code:
PHP:
$txtVar = "My text";
JavaScript:
var txt = '<?php echo $txtVar; ?>';
document.getElementById('MyDiv').innerHTML = txt;
HTML(result):
<div id="MyDiv"><!--?php echo $txtVar ; ?--></div>
I just want the string value to be printed in my html, withou the comments ()
First print the PHP variable value in another HTML entity like hidden input HTml tag and after that pick the hidden value using JavaScript and assign into your desire tag.
In Your page.
<input type="hidden" value="<?php echo $txtVar; ?>" id="phptext" name="phptext" />
JavaScript code:
document.getElementById('MyDiv').innerHTML = document.getElementById('phptext').value;
This is works.
Here is your answer.
<?php
$txtVar = "My text";
?>
<div id="MyDiv"></div>
<script>
var txt = "<?php echo $txtVar; ?>";
var element = document.getElementById('MyDiv');
element.innerHTML = txt;
</script>
If your javascript is an external file, you can just rename it with .php extension, and then retrieve it with
<script language='javascript' type='text/javascript' src='yourscript.php'></script>

Categories

Resources