2

People, i've tried many tutoriais but none of then seens to work for me, maybe I'm doing something wrong, not sure, but let's go.

I've this page with some visible and not visible content. They are controled by a menu, which triggers a function that displays and hides the content. Ex: Life, Economy and Hardware. If the user clicks on Economy, all Economy articles and tab options will become visible as the other ones, be hided. That's is working just fine, but, on my homepage there are also some buttons with this options (life, economy and hardware) that shares a URL and directs the user to this page with an URL #ID argument:

<button href="mysite.com/exams/articles/#v-pills-1">
<button href="mysite.com/exams/articles/#v-pills-9">
<button href="mysite.com/exams/articles/#v-pills-17">

How can I make this /life.php page get the #v-pills-0 ID on the URL and identify the number, so I can work a function based on it? Thank you very much for any response.

  • What have you tried so far? – steven7mwesigwa Jan 28 '22 at 15:54
  • A lot of options with JS and PHP. (https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) , (https://www.geeksforgeeks.org/how-to-get-parameters-from-a-url-string-in-php/) and many others. Tried different aproachs with Parset JS (guess that's how is written) and ways of getting a URL argument with PHP – Lucas M. Falbo Jan 28 '22 at 15:58
  • 1
    If I understand what you are asking, you can't. The browser does not send the fragment part of the url to the server when a user click on a link or a button. You have to resort on javascript or change the url you are using to avoid the fragment part (the chars after the #) – Eineki Jan 28 '22 at 16:00
  • I've tried also this method: https://stackoverflow.com/questions/41263261/get-id-from-url-in-a-variable-php , but can't figure out how to make it work for me. – Lucas M. Falbo Jan 28 '22 at 16:00
  • PHP can't read after the Hashtag. You must read it from javascript and pass it to php via parameter... or change the url to something like this: mysite.com/exams/articles/?id=v-pills-1 You can also use rewrite rules on the .htaccess in order to get rid of ? symbol and get urls like this: mysite.com/exams/articles/v-pills-1/ – Leandro Ferrero Jan 28 '22 at 16:00
  • Can't because this #v-pills-0 is a bootstrap tab ID and needed to another component.. – Lucas M. Falbo Jan 28 '22 at 16:03
  • How can I make it with JS? Get this argument and applies a function based on it? – Lucas M. Falbo Jan 28 '22 at 16:04
  • 1
    On the destination page, you can add a JS code on the load event, to read the hashtag and select the correct tab, Look at this: https://stackoverflow.com/questions/1822598/getting-url-hash-location-and-using-it-in-jquery – Leandro Ferrero Jan 28 '22 at 16:07
  • 1
    You could send the hashtag fragment section to the server as whereas retaining the functionality of hashtags on URLs. I.e: ` – steven7mwesigwa Jan 28 '22 at 16:08
  • So what you want is to get #v-pill-0 from the URL and make the bootstrap tab with href="#v-pill-0" active? – ruleboy21 Jan 28 '22 at 16:12

1 Answers1

1

Done it!

Thanks to the steven7mwesigwa comment, thanks bro!

I changed the button href to:

<button href="mysite.com/exams/articles/?id=v-pills-1#v-pills-1">

Mantaining the #ID funcionality and making it possible to get the id= with PHP, since steven said PHP can't get the #value without the ?= parameter.

And used the following PHP code that I've tried before but didn't work.

$argumento = $_GET['id'];

Thanks to everyone.

  • 2
    `#v-pills-1` is completely superfluous. – Martin Zeitler Jan 31 '22 at 00:05
  • No, it's not. The #v-pills-1 is working together with an jquery script to make sure that, when the client opens the page, the right bootstrap TAB will be showed. The ?=id is for the PHP function, and the #v-pills for the JS Bootstrap function. – Lucas M. Falbo Feb 02 '22 at 14:36