Passing php var with JavaScript [duplicate] - javascript

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>

Related

How can I use PHP in JavaScript? [duplicate]

This question already has answers here:
How to get the pure text without HTML element using JavaScript?
(10 answers)
How do I change the text of a span element using JavaScript?
(18 answers)
Closed 3 years ago.
I have to show the contact num from database when user click on the button..
but when I use php in javaScript it doesn't work for me...
here is HTML
<button name="showNo" id="phone1" onclick="myFunction()"><span>Show Phone No</span></button>
<p id="show">******</p>
here the JavaScript
<script type="text/javascript">
function myFunction(){
var hsh = document.getElementById("show");
if ( hsh.value === "******" )
hsh.value = "<?php echo $contact; ?>";
else
hsh.value = "Open Curtain";
}
</script>
is there any other method to do this,,?
Try this, p tag does not have value so you need to use innerHTML to get data from it and set it
var hsh = document.getElementById("show").innerHTML;
if(hsh === "******"){
document.getElementById('show').innerHTML = "<?php echo $contact; ?>";
}else{
document.getElementById('show').innerHTML = "<?php echo 'Open Curtain'; ?>";
}

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

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');

Unable to show php variable in javascript?

I am unable to show php variable in javascript;
this is my code here:
<script type="text/javascript">
$(document).ready(function (){
var n=<?php echo json_encode($count)?>;
for(var i=0;i<n;i++){
var div = document.createElement('div');
div.className = "d5";
div.id=i+1;
document.getElementById('wrapper').appendChild(div);
<?php
$query="select * from shop_product where shop_uniqueid='$unq'";
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
$product=$row["in_product"];
?>
var product=<?php echo $product?>;
$('#'+div.id).html(product);
<?php
}
?>
}//for loop ends
});//ready ends
</script>
here i am trying to pass var product in html() of which value is coming from php like: var product=<?php echo $product?>;
but when doing so php value is not coming in Javascript's var product.
when I pass $('#'+div.id).html("abcd"); abcd value is showing in divs.
please help me.
Compare the first place you take data from PHP and put it in JavaScript:
var n=<?php echo json_encode($count)?>;
with your attempt to assign the product value:
var product=<?php echo $product?>;
You haven't converted the data from plain text to JavaScript in the second line. Use json_encode there too.

Javascript variable not passing to php variable [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I have been trying to pass javascript variable to php and it wont work dynamically, but works statically.
here is the dynamic code that does not work:
<input onClick="myFunction();" id="demoteTask" name="demoteTask" type="checkbox"/>Demote to Child
<script>
function myFunction() {
var parent = prompt("Please enter parent reskb id:", "");
<?php $new = "<script>document.writeln(parent);</script>" ?>
alert (<?php $new ?>);
}
</script>
here is the static code that worked:
<script>
var p1 = "hello";
</script>
<?php
$kk="<script>document.writeln(p1);</script>";
echo $kk;
?>
The dynamic code returns me null value in the alertbox.
Why you're mixing server side and client side scripts.
Server side scripts executes and sent packets to client.
Here in your code PHP will generate output as:
THIS
<input onClick="myFunction();" id="demoteTask" name="demoteTask" type="checkbox"/>Demote to Child</input>
<script>
function myFunction() {
var parent = prompt("Please enter parent reskb id:", "");
// Here `$new` becomes server variable
<?php $new = "<script>document.writeln(parent);</script>" ?>
// and will prints here
alert (<?php echo $new ?>);
}
</script>
TO THIS
<script>
function myFunction() {
var parent = prompt("Please enter parent reskb id:", "");
alert (<script>document.writeln(parent);</script>);
}
</script>
FIX
Your should only use client side script. And there is no need to mix scripts. Server scripts can generate client scripts as server scripts variable. Here is my example:
<script>
function myFunction() {
var parent = prompt("Please enter parent reskb id:", "");
alert(parent);
}
</script>

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; ?>";

Categories

Resources