0

I have a html page, and php script that is action of a form on the html page. I want to be able to get the last bit of text after the hash of the previous url (from the html page)

www.website.com/htmlpage#token5

So that I get just have the string: "token5" placed into a varible so that when I submit the html form, the PHP script gets the previous URL or something on those lines, to be able to get this string.

something like:

1. submit form from www.website.com/htmlpage#token5
2. action of form goes to www.website.com/phppage
3. php page gets the "token5" string.

How would I go about doing this? thanks`

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Joseph Jones
  • 187
  • 3
  • 10

3 Answers3

0

You could use JavaScript to get the hash and latter add it to your form element.

  if(window.location.hash) {
      var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
      // do whatever you want to do with hash
  } else {
      // No hash found
  }
Arun
  • 136
  • 1
  • 10
0

Please take a look at:

Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?

#token5 will never be passed to the server.

What you can do is put the value of #token5 into a hidden input:

<input type="hidden" value="token5" name="token"/>

Then server side PHP depending on how you post your form you can do this:

// If your action on form is 'post'
$token = $_POST['token'];

or

// If your action on form is 'get'
$token = $_GET['token'];
giolliano sulit
  • 996
  • 1
  • 6
  • 11
  • Hmm. So how would PayPal with their paypal.me website automatically fill in a field based on the URL e.g. paypal.me/payperson/15 15 being the amount to put into the field – Joseph Jones Aug 01 '17 at 06:25
  • So if you're doing just `/15` that will work as there's no hash (#) in front. If you want to post a value in the URL you can do this: `www.website.com/htmlpage?token=5`. Then in your PHP file you can get the value by using: `$token = $_GET['token']`. – giolliano sulit Aug 01 '17 at 06:30
0

The easiest way perhaps to break a url into it's constituent parts would be to use parse_url

$url='http://www.website.com/htmlpage#token5';
$parts=parse_url( $url );
echo '<pre>',print_r($parts,true),'</pre>';

/* or */

$hash = parse_url( $url, PHP_URL_FRAGMENT );
echo $hash;

Will output

Array
(
    [scheme] => http
    [host] => www.website.com
    [path] => /htmlpage
    [fragment] => token5
)

token5
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • there was no mention of paypal in the question. I understood the question to mean that you wanted to take the token from a url and assign as a variable - which is what the above does ~ please clarify – Professor Abronsius Aug 01 '17 at 06:25