Using a Javascript variable inside a php block [duplicate] - php

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>

Related

how to pass variables in url with an ID section using PHP? [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 9 years ago.
I can do this:
home
But when I have a link with an Id, I can't pass variables:
home <--!this doesn't work of course -->
Thanks a lot!
When using PHP you can not use '#' in the URL, it will not be passed to the server.
You can use urlencode in order to encode the non-alphanumeric characters.
Use window.location.hash in javascript
<script>alert(window.location.hash);</script>
And the parse_url() function in PHP
<?php echo parse_url("home.php?var=home#sectionID",PHP_URL_FRAGMENT);?>
your HTML will be..
home
Declare a variable with $home dollar sign, i think you getting confused with JavaScript variable and PHP. To retrieve the variable values on a new page use $_GET['ID'].
Hope this helps.

How to print address url including # [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 9 years ago.
I want to echo exact url : http://domain.com/mystuff.html#123456
but it only print http://domain.com/mystuff.html how to include the #123456 ? so become http://domain.com/mystuff.html#123456
my code so far
$urladdress = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?".$_SERVER['QUERY_STRING'];
You cannot. The fragment identifier is handled entirely client side so the browser will not send it to the server.
(The nearest you could come would be to (after the page has loaded) use JavaScript to sent it to the server in another request.)
you cannot get it using php, you can get it done by javascritp. use this test script to check your result.
<script>
alert(document.URL);
var urlDetail = document.URL;//you can use this variable to print in your web page.
</script>
Found the answer:
text

php array to javascript [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)
I have a PHP array that I want to use in a javascript script. I suppose that I will have to convert it to a string that can be understood by javascript, and embed the string inside the script. What is the best way to accomplish this.
EDIT - This is for initial load of the page, not subsequent AJAX call
$textMessages = array();
You could use JSON.
json_encode and json_decode on the server side and JSON.stringify and JSON.parse on the client side. Both client side functions are natively built-into modern browsers but if you need to support legacy browsers you could include the json2.js script to your page.
json_encode(). Put in a PHP array, receive a string representation of a JS array.
JSON is perfect for this.
echo json_encode($textMessages);

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.

Render a PHP code using jQuery [duplicate]

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.

Categories