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];
Related
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
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 5 years ago.
I'm using
location.hash = $(this).attr("id");
to append id into url bar immediately without reloading. the outcome is : http://example.net/index.php/abc#id
then I echo
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
the output I got was : http://example.net/index.php/abc
How can I get the #id (known as fragment id ) in PHP so that it can be used as PHP variable ?
Thank you!
The webserver don't receive the hash fragment, it's client side only. So you cannot get it with PHP or any other server side language. You can hack around by fetching that hash via javascript and sending it via a GET parameter to the server side script but i don't recommend this.
You can not directly send the fragment part from client to the PHP server side. But the fragment id can be accessed through javascript and you can send it to server as a GET parameter.
Use window.location to get the full url including fragment part and window.location.hash to get just the fragment part in javascript.
In case you send the full url (including fragment hash) to the PHP server, you can use parse_url("URL HERE",PHP_URL_FRAGMENT) function to get the fragment hash.
This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
I have a jQuery function, which, on click, itercepts a clicked link:
$("a").click(function(e) {
e.preventDefault();
var link = $(this).prop('href');
alert(link);
});
and it works properly, but how to pass that link to a PHP variable to 'echo' it later?
I unsuccessfully tried:
$("a").click(function(event) {
event.preventDefault();
var link = $(this).prop('href');
<?php $link = .'link'. ?>
});
I know it may be a silly question, I looked all over the web but I didn't find any proper answer.
The only way to do this is to have a script wich stores values passed to it in a session or to a DB and either output the session data later or read from the db
$("a").click(function(event) {
event.preventDefault();
var link = $(this).prop('href');
$.post('saveLink.php', {href : link});
});
the saveLink.php would be something like this
<?php
// Clean your POST variable, always clean any POST variables
// see here for a good discussion on that
// http://stackoverflow.com/questions/4223980/the-ultimate-clean-secure-function
$link = htmlspecialchars($_POST['href']);
$_SESSION['links'] = $link;
?>
then later on you can retrieve all you session data
Note
Session data is not permanent and not shareable between users so if you need to do that you would be better off with a db query also if you need to access a particular link then you will need to send some sort of id with it.
PHP is preprocessor running on a server side. jQuery is running on client side (in user's browser) and it can't comunicate with PHP like this.
Although you can send the data to PHP with Ajax request and process them in another php script, for example store them in a database.
$.ajax({
url: '/ajax_catch.php',
type:'POST',
data: {
link: link
},
success: function(m){
//what to do after its done
}
});
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]);
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I am trying to create a site where someone can create an "item" and the database will store an id and php generates a url for that id. So next time when the person comes back with that url it will remember the person's settings (variables). Now the problem is that in my site javascript need to know these variables.
So what is the best solution for this? passing the variables in the superglobal "GET" or maybe cookies? Or is there a better way to pass these variables to javascript?
just use php to print some dynamic javascript
<script>
var myVar = "<?php echo json_encode($_COOKIE['somevalue']);?>";
</script>
There are multiple methods for providing the data to the client, such as:
Echo the variables in your javascript, var userid = <?php echo
$userid; ?>
JSON'fy your variables and supply them to your javascript via AJAX/jQuery: $.getJSON(url, function(data){ var userid = data.userid; });
I typically utilize JSON as much as possible when trying to present server-side data to the client-side, as it helps to separate the different layers.