Something similar like <?php echo $_GET['height'];?> in HTML? [duplicate] - javascript

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
im wondering if there is a function or something like php's <?php echo $_GET['height'];?> but in HTML or javascript.
I need it to get variables of the URL

Use location.search. It returns the entire query string, which you then can split, if you need to.

Html is a markup language, so you can't do such things.
Try using javascript.
here's an example:
//returns the height of the client
function height(){
var clientHeight = document.body.clientHeight;
alert(clientHeight);
}

Related

How to include a PHP variable in Javascript code? [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 5 years ago.
[1] http://imgur.com/a/Ny8f7 "html code"
[2] http://imgur.com/a/kzEN2 "php code"
I try to use the variables from php in javascript to generate a chart with them.
Try using an id e.g.
In your html
name='someName'>
Then use that ID in your JS.
Use can use something like this:
var data = '<?=$dataFromPhp?>';

Need help breaking down JSON string in PHP [duplicate]

This question already has answers here:
How to convert JSON string to array
(17 answers)
Closed 7 years ago.
I need to be able to grab some data from a string using PHP.
I got an API from the games website and need to break it down.
The string I need to break down is this:
http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513
I need to get the small icon image from that string which is the first url in the string, and the current price, which in the string is 887.
So, where it states this:
"current":{"trend":"neutral","price":887}
I need to grab the 887 and put it into a variable.
I'm using PHP,
thanks in advance if anyone can help :)
Use json_decode() for this purpose:
$item = json_decode($json)->item;
$price = $item->current->price;
$icon = $item->icon;
Download the JSON and convert to an object.
$item = json_decode(file_get_contents('http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513'))->item;
$name = $item->name;
// etc.
Here's a function you should keep handy. It is useful for printing formatted objects and arrays, which makes it much easier to examine their structures.
function debug($v) {
echo '<pre>';
print_r($v);
echo '</pre>';
}

Javascript equivalent of PHP's "print" [duplicate]

This question already has answers here:
Is there a php echo/print equivalent in javascript
(9 answers)
Closed 8 years ago.
If I have some PHP HTML that reads like this:
<html> I am feeling <?php print urldecode($_GET["emotion"]);?> today!</html>
and Get's "emotion" in URL title is "Happy", the HTML renders "I am feeling Happy today!".
Now, migrating away from PHP, how do I do this with javascript?
In Javascript I have a variable $emotion = "Happy"; so what Javascript goes inbetween the script tags below (where there are currently asterisks********)
<html> I am feeling <script>*********************</script> today!</html>
You can do it easily by putting an element where you want the text to change, then accessing that element by it's id.
$emotion = "Happy";
var e = document.getElementById('emotion');
e.innerHTML = $emotion;
I am feeling <span id="emotion"></span> today!

PHP $_Get and JQuery .load [duplicate]

This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 8 years ago.
I want to load another page via Javascript like this
$("#resultarea").load("searchresults.php?searchstring=" + $("#searchbox").val());
in searchresults.php, a single statement
echo $_GET["searchstring"];
what I type in searchbox appears when searchresults.php is loaded except when I add space and another word, no thing appear at all. I heared it can be done by encoding or somewhat, I searched but didn't find a solution.
Try encodeURIComponent:
var encodedValue = encodeURIComponent($("#searchbox").val());
$("#resultarea").load("searchresults.php?searchstring=" + encodedValue);
Source
Demo

Injecting PHP data into JavaScript code [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
What's the preferred way of outputting PHP code within apostrophes? For example this JavaScript code would break and the second line will be totally empty:
var jsString = '<div id="content">'+'<?php echo $something['1'] ?>'+'</div>';
Thanks! I know this is kind of basic and I know how to fix it, but I'm wondering how to do it the right way?
[edit]
I know it looks obvious, but $something["1"] won't work. The same goes for $something[\'1\'].
This example is working well for me :
<?php
$something['1']='hhhh0';
?>
<script type='text/javascript'>
var jsString = '<div id="content">'+'<?php echo $something['1'] ;?>'+'</div>';
alert(jsString);
</script>

Categories

Resources