07
2011Get URL Hash Parameters & Values using JavaScript for PHP $_REQUEST
Tags: javascript
We all know that php can read the url values of query string variables using $_REQUEST but foi yuo need to get the values of hash, PHP can’t really help. Some suggest to use parse_url() function but it can’t get the fragment values. Reason for this is that the browser won’t even send a request with a fragment part. The fragment part is resolved and remains in the browser, i.e. on client side.
An example of hash variables in url: http://www.domain.com#image=some.jpg&status=success
And since it’s on the client side, hash variables from the url are accessible using JavaScript. Once inside the Javascriot, we can easily send them in the form of query string to the php script to be retrieved using $_REQUEST.
Get parameter values from URL string of the current page that was constructed and sent by PHP script.
// Read a page’s GET URL variables and return them as an associative array.
function getHashUrlVars(){ var vars = [], hash; var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&'); for(var i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); vars.push(hash[0]); vars[hash[0]] = hash[1]; } return vars; } // Get all URL parameters var allVars = getHashUrlVars();
The function returns an array with your URL parameters and their values. For instance, you have this URL to parse:
http://www.domain.com#image=some.jpg&status=success
// Getting URL var by its name var image = getHashUrlVars()["image"]; var status = getHashUrlVars()["status"]; var data = "process.php?image="+ image +"&status="+status; alert('imgURL: '+unescape(image)); alert('status: '+unescape(status)); //redirect the page to php script to be accessed using $_REQUEST window.location = data;
Hope that helps.
Cheers!
Related posts:
- Displaying Global Configuration Settings Values in Joomla Website
- [AS3] Passing variables from HTML using SWFObject JavaScript to Flash
- How to remove “setting session.bug_compat_42 or session.bug_compat_warn” Warning
- Resolve Mobile Safari Greyed out File Field Issue in iPhone/iPod/iPad by using Picup App to upload images
- PHP Function to Clean Data before using in JavaScript or FBJS
Recent Comments
(8 hours ago)
(8 hours ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)