Render a PHP code using jQuery [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)
I got this problem rendering or making a PHP code functional using jQuery. huhu.
Can someone help me?
Here's the code I wanted to do:
document.getElementById("fade").innerHTML = '<?php echo 'SOME-PHP-CODE-HERE'; ?>

Try using
document.getElementById("fade").innerHTML = '<?php echo $var; ?>';
Check if $var has no quotes. If it has, you need to escape them using addSlashes()
And if you are using JQuery you can better write
$('#fade').html('<?php echo $var; ?>');

Try:
document.getElementById("fade").innerHTML = '<?=$php-variable;?>
Make sure it is embedded javascript and not external. (Between <script></script> tags in the .php file)

It is hard to tell, what you are asking for. But assuming you want to display some PHP code within some element and you want to do this from JavaScript, this is some solution for you:
Download PHP.js script and place it on your site, then include it so it is available from other JavaScript code.
Second, use htmlentities() function on the content you want to show (the string containing PHP code). This may look like the following:
var phpCode = '<?php echo \'something goes here\'; ?>';
document.getElementById('fade').innerHTML = htmlentities(phpCode);
As a proof that it works, see this jsfiddle.

Related

How can I put an opening and a closing PHP tag in an echo? [duplicate]

This question already has an answer here:
How can we include php tag in html with php echo [closed]
(1 answer)
Closed 10 years ago.
I already post this question before but people always ask unnecessary question. I'm gonna explain it in a simple way.
I HAVE 3 files :
a php file (contains only html, thats important) : We call it X file for the example.
a php file where there's some database query to insert database data on the screen : Y file
a php file (a script that will make some manipulations) : Z file
SO, i want to include Y into X with the script of Z.
In Z, i make a str_replace($text, $new, file_get_contents($file));
The ONLY THING is that i need to include PHP open and close TAGS in X because there's no php tags in it.
So, $new = "<?php include('Y.php'); ?>";.
If you try, the close tag wont be considered in the string, but that's what i want.
Hope this question is NOW clear. I can't be more clearer than that. :D
Thanks for you advice.
You need to replace the < and > characters with their HTML entities:
<?php echo "<?php include('this.php'); ?>"; ?>
If you want to include the file, there's no need for the above, running the code below is more than sufficient:
<?php
include('this.php');
?>
You don't have to use two lots of PHP tags...
I may as well put this as an answer.
If you don't want to manually replace < and >:
<?php echo htmlentities("<?php include('this.php'); ?>"); ?>

pass php variable value to javascript [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
Im trying to pass the value of a php variable into javascript but i just cant get it to work. Am I doing anything wrong? Below is the line of code I'm working with.
var dist = parseInt("<?php echo json_encode($distance); ?>");
$distance is an integer? why don't you just write
var dist = <?php echo $distance; ?>
If the value in $distance is just an integer, you don't need the json_encode call. You can just do a php echo of $distance.
Something like
var dist = <?php echo $distance; ?>;
if you right click > view html source in your web browser, you would see for yourself that you have an extra set of quotes.
And, good for you for using json_encode() to output it as a string. That's an excellent way to safely output a value to javascript. although, if its an integer, theres no need here.

Using a Javascript variable inside a php block [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)
Access a JavaScript variable from PHP
I have a Javascript function that takes in one variable. The function has some php code inside it. I want to use this variable inside the php section of the function. I couldn't get it to work. How is it done?
The issue is that your PHP code is being rendered on the server before being served to the client. I would recommend either converting the PHP code into Javascript code or creating an AJAX call to the PHP function.
Start reading about AJAX! You will likely need to rewrite some of the code you have written but what you are attempting to accomplish is not really possible otherwise.
try with it
<script>
var whatever = "<?php echo $phpVar ?>";
</script>

Passing PHP variable in a javascript function [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
<button class="addToPlaylist" onclick="javascript:myPopup(<?php echo $videos[$counter]?>);
return false;">+</button>
I have a button on an image as a html hyperlink. I want to perform different actions on hyperlink and button. The above code works whenever I do not pass the PHP variable using echo. When i pass PHP variable, the button also performs the same action as of the hyperlink, that means return false does not work.
Any idea why the return false; does not work when i pass PHP variable?
This should be:
<button class="addToPlaylist" onclick="javascript:myPopup('<?php echo $videos[$counter];?>');return false;">+</button>
Note the single quotes in myPopup. As you pass a string to myPopup, you will need to enclose it with single quotes. (Double won't work as there is already double quotes for the onclick)
I am quite sure $videos[$counter] is not numeric, but a string. In this case you have to write the quotes:
onclick="javascript:myPopup('<?php echo $videos[$counter]?>');
And make sure, $videos[$counter] doesn't contain any, something like
onclick="javascript:myPopup('<?php echo addslashes($videos[$counter])?>');
comes to mind.
onclick="javascript:myPopup("";return false;" . This should work and i think it's more clear where you have javascript code and php code.

Access PHP variable in JavaScript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?>
Is there any way to get access to a PHP variable in JavaScript?
I have a variable, $a, in PHP and want to get its value in a JavaScript variable.
You can't, you'll have to do something like
<script type="text/javascript">
var php_var = "<?php echo $php_var; ?>";
</script>
You can also load it with AJAX
rhino is right, the snippet lacks of a type for the sake of brevity.
Also, note that if $php_var has quotes, it will break your script. You shall use addslashes, htmlentities or a custom function.
metrobalderas is partially right. Partially, because the PHP variable's value may contain some special characters, which are metacharacters in JavaScript. To avoid such problem, use the code below:
<script type="text/javascript">
var something=<?php echo json_encode($a); ?>;
</script>
I'm not sure how necessary this is, and it adds a call to getElementById, but if you're really keen on getting inline JavaScript out of your code, you can pass it as an HTML attribute, namely:
<span class="metadata" id="metadata-size-of-widget" title="<?php echo json_encode($size_of_widget) ?>"></span>
And then in your JavaScript:
var size_of_widget = document.getElementById("metadata-size-of-widget").title;

Categories