$.getJSON PHP alternative [duplicate] - php

This question already has answers here:
Get JSON object from URL
(11 answers)
Closed 2 years ago.
$.getJSON might be misleading, but what I want to do is get JSON from a url via a PHP file.
var url = "https://script.google.com/macros/s/XXXXXXXXXX/exec?action=read";
$.getJSON(url, function (json) {
//DO SOMETHING WITH THE DATA
}
This works fine in javascript. When I google get JSON from url maybe it's because I'm learning PHP but I can't seem to find an answer that works. Or when I google $.getJSON I get people trying to get Json from a PHP file.
All my data is stored via a googlesheet that I access by calling google script and it sends it back and I can take the information I want e.g
json.records[0].NAME;
I want to pass some data to a PHP file that I can't fathom how to do that.
Any help would be greatly appreciated.

It's simple as js:
$content = file_get_contents('https://script.google.com/macros/s/XXXXXXXXXX/exec?action=read');
$data = json_decode($content); // return object
$data = json_decode($content, true); // return array

Related

how to get #jumper part from url in codeigniter? [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 7 years ago.
My Url is
http://www.domain.com/seg_one/seg_two/seg_three#jumper
I want to get #jumper part of current url
In JavaScript, you can get this by simply location.hash property of window object. i.e.
window.location.hash; // will give you #jumper.
From here once you have it on the client side, do anything you want with it. You can send it back to server by even making an ajax call.
The # is called a fragment. The problem is that browsers won't transmit those to the server so there is now way to parse it.
You can get it via javascript (see below) and make an ajax request to use it in the back-end(PHP):
window.location.href
You can condition the ajax call:
address = window.location.href;
index = address.str.indexOf("#");
if(typeof index !='null') {
var term = str.slice(address.str.indexOf("#")+1, address.length);
console.log(term);//will display jumper
//send it via AJAX
}
$third = $this->uri->segment(3);
$thirdArr = explode('#', $third);
$hash = $thirdArr[1];

PHP - How to get last segment of URL string [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'm trying to extract the last segment of this URL: http://test.com/blog/#segmentIwant (the #segmentIwant is the string I want). The hash tag is generated from a link within the website. Any help is much appreciated!
PHP cannot read the hash, the server doesnt even recieve it at all!
Well while you load a page with # tag, php does not know it as # tags on url are treated by basically javascript and are not sent to server. But with a simple AJAX request you can send the value to server on loading the page and get the response through that AJAX response.
To grab the url with Javascript:
var url = document.URL;
And try basename() function to get the last part of the sent url with PHP code.
echo basename( "http://test.com/blog/#segmentIwant" );
Or a simple function in javascript to do it:
var url = document.URL;
hashed_string = url.split("#");
alert(hashed_string[1]);

How to dynamically copy array from php to javascript? [duplicate]

This question already has answers here:
PHP array to javascript array
(4 answers)
Closed 9 years ago.
I'm writing code in PHP and HTML for the server side coding of a GPS tracking system. The PHP code will access the coordinates of the GPS from a database. I've found HTML code to display those coordinates on a map(as a Polyline). In the database, a new tuple will be added almost every minute, so we cannot specify how many elements are in the array, before hand.
How do I dynamically copy an array from php to javascript?
this is the website with the code for drawing the polyline.
How do I dynamically copy an array from php to javascript?
You can’t “copy” it at all, because PHP and JavaScript are two completely different worlds.
What you can do is transfer it in text form – for example by encoding it as JSON in your PHP script, echo-ing it out as JS code (or request it from the server in a new HTTP request via JSONP/AJAX), and parse it back into a JavaScript object or array structure.
Give something like this a try:
<?php
$arr = array('cat', 'dog', 'mouse');
?>
<body>
<script>
var jsArr = <?php echo json_encode($arr); ?>;
console.log(jsArr);
</script>
</body>
Should output
["cat", "dog", "mouse"]
in console

Passing form data to php with json [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using value passed to php by jQuery.getJSON
Hi I have this code which is passing data through:
var output = [];
$.getJSON(
'DisplayMap.php',
{ runDate : $('input[name="date"]').val() },
function(data) {
for (var i in data.b) {
output.push(new google.maps.LatLng(data.b[i].Ya, data.b[i].Za));
}
}
);
My question is, how would I handle that data and assign it to a php variable. Would I have to decode it?
Thanks in advance
You will be able to access it via
$_GET['runDate'];
Side note: For Google Maps API, don't access Ya and Za directly. They are not always consistently named. Instead, use .lat and .lng attribute getters
For example:
new google.maps.LatLng(data.b[i].lat, data.b[i].lng);

move var from javascript/jquery to php script on same page [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to pass JavaScript variables to PHP?
how would I go about moving a javascript variable over to a php script without a form submission?
is it even possible?
ajax is an acceptable option but how
lets say I want to run thisnewoption.php in process sending it javascript variable imgfilename which contains a string
Once your php page has loaded, there's no going back. The best way to go about passing variables ( data ) back to the server is with ajax, so it might be time to brush up on ajax http://api.jquery.com/jQuery.ajax/
Update: Tell you what, here is what a really basic ajax call looks like, notice that is send data to the server here with POST:
$.ajax ( {type : 'POST' ,
url : 'email.php' ,
data : { variable: variable, anotherVariable: anotherVariable } ,
success : function ( data ) { $('#some-div').html ( data ) ; }
} ) ;
You cannot literally pass a javascipt variable as a PHP one as PHP is evaluated on server side where as javascript on client. You can use ajax call to pass it however.

Categories