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: cjay175 is an unknown quantity at this point 
Solved Threads: 0
cjay175 cjay175 is offline Offline
Newbie Poster

Get hash value & pass from javascript to php

 
0
  #1
Oct 25th, 2009
Hey,

I was wondering how to obtain a hash value and pass it to php.

URL:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. 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)
  1. <script>
  2. function getUrlVars()
  3. {
  4. var vars = [], hash;
  5. var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
  6. for(var i = 0; i < hashes.length; i++)
  7. {
  8. hash = hashes[i].split('=');
  9. vars.push(hash[0]);
  10. vars[hash[0]] = hash[1];
  11. }
  12. return vars;
  13. }
  14. </script>

This is how I am going to call the value of the hash:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. 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)
  1. $refer_id = $_GET['refer']; //<-- Grab this from javascript (the value of the hash)
  2. $days = 5;
  3.  
  4. $date_of_expiry = time() + 60 * 60 * 24 * $days ;
  5. setcookie( "referrer", $refer_id, $date_of_expiry );

Thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 102
Reputation: jomanlk is an unknown quantity at this point 
Solved Threads: 18
jomanlk jomanlk is offline Offline
Junior Poster
 
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 20
Reputation: cjay175 is an unknown quantity at this point 
Solved Threads: 0
cjay175 cjay175 is offline Offline
Newbie Poster
 
0
  #3
Oct 26th, 2009
Hey jomanlk

thanks for the advice, after reading here for a bit I see that passing variables from javascript to php can mostly onyl be done with a form or cookie.

So I will just use javascript to get hash value and set cookie and than retrieve it later with php.

thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 885
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 127
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark
 
0
  #4
Oct 26th, 2009
Cjay,
Originally Posted by cjay175 View Post
So my question is, first is this the proper javascript to grab the hash value?
This is actually very simple. Javascript makes the hash string availabnle as 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 #.

Originally Posted by cjay175 View Post
Second how would I pass the hash value to a php variable? (the variable is going to be used to set a cookie)
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
Last edited by Airshow; Oct 26th, 2009 at 11:45 pm.
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 885
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 127
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark
 
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
Last edited by Airshow; Oct 27th, 2009 at 12:00 am.
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 806 | Replies: 4
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC