| | |
Get hash value & pass from javascript to php
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: May 2009
Posts: 20
Reputation:
Solved Threads: 0
Hey,
I was wondering how to obtain a hash value and pass it to php.
URL:
All I want to grab is just the "110".
This is my javascript code:
This is how I am going to call the value of the hash:
So my question is, first is this the proper javascript to grab the hash value?
Second how would I pass the hash value to a php variable? (the variable is going to be used to set a cookie)
ie:
Thanks
I was wondering how to obtain a hash value and pass it to php.
URL:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
http://www.mydomain.com/#id=110
All I want to grab is just the "110".
This is my javascript code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script> function getUrlVars() { 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; } </script>
This is how I am going to call the value of the hash:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var first = getUrlVars()["id"]; // <-- name of hash
So my question is, first is this the proper javascript to grab the hash value?
Second how would I pass the hash value to a php variable? (the variable is going to be used to set a cookie)
ie:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
$refer_id = $_GET['refer']; //<-- Grab this from javascript (the value of the hash) $days = 5; $date_of_expiry = time() + 60 * 60 * 24 * $days ; setcookie( "referrer", $refer_id, $date_of_expiry );
Thanks
•
•
Join Date: Oct 2009
Posts: 102
Reputation:
Solved Threads: 18
0
#2 Oct 26th, 2009
It would be better if you use a query string instead of a variable. So that PHP can directly access it using $_GET. Failing that, you could make an ajax call to send a request to PHP file with the hash id as a parameter. Only reason you should be using this method would be if the URL pattern was beyond your control.
There might be a way to do this through .htaccess as well by rewriting the URL. Someone knowledgeable in that area could help you out.
There might be a way to do this through .htaccess as well by rewriting the URL. Someone knowledgeable in that area could help you out.
0
#4 Oct 26th, 2009
Cjay,
This is actually very simple. Javascript makes the hash string availabnle as
It depends what you are trying to do, but the easiest thing would be to write the cookie directly from Javascript. There's no point me desribing how to do this here as Javascript cookies are well documented elsewhere - here for example.
I believe that a cookie set in Javascript is identical in function to a server-originated cookie. Though I've never tried it, you should (at least in theory) be able to set a cookie value from Javascript and read it in PHP (encryption permitting).
Airshow
•
•
•
•
So my question is, first is this the proper javascript to grab the hash value?
location.hash . The js standard specifies that the leading # should be included but not all browsers honour this. Therefore use location.hash.split('#')[0] for the hash string without the leading #.•
•
•
•
Second how would I pass the hash value to a php variable? (the variable is going to be used to set a cookie)
I believe that a cookie set in Javascript is identical in function to a server-originated cookie. Though I've never tried it, you should (at least in theory) be able to set a cookie value from Javascript and read it in PHP (encryption permitting).
Airshow
Last edited by Airshow; Oct 26th, 2009 at 11:45 pm.
50% of the solution lies in accurately describing the problem!
0
#5 Oct 26th, 2009
Cjay,
I have just re-read your question and I don't think you mean hash. I think you are trying to parse the "query" (aka "search") part of the url to separate out the parameters that appear in php in the $_GET array.
For this, you need to read my Query Parser code snippet.
Airshow
I have just re-read your question and I don't think you mean hash. I think you are trying to parse the "query" (aka "search") part of the url to separate out the parameters that appear in php in the $_GET array.
For this, you need to read my Query Parser code snippet.
Airshow
Last edited by Airshow; Oct 27th, 2009 at 12:00 am.
50% of the solution lies in accurately describing the problem!
![]() |
Similar Threads
- pass variable from php to javascript (PHP)
- NEED help to pass javascript variable to php (PHP)
- how do i pass arrays in javascript to php using POST method (JavaScript / DHTML / AJAX)
- pass the javascript variable to php page (PHP)
- Pass a javascript variable to PHP in the same function (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: appendChild, invalid child type?
- Next Thread: toggle checkbox group when the max limit is reached
Views: 806 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
ajax ajaxexample ajaxjspservlets array autoplay blackjack browser captchaformproblem checkbox child class close codes date debugger dependent developer disablefirebug dom editor element embed engine events explorer ext file firefox flash form forms game gears getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl maps margin marquee masterpage math matrixcaptcha media mysql object onerror onmouseoutdivproblem onreadystatechange parent passing paypal pdf php player position post programming rated redirect runtime safari scriptlets scroll search security session shopping size software solutions star stars stretch synchronous tweet unicode variables web webkit webservice window wysiwyg \n





