-5

In my page I have a bunch of instances where (using php) I have links sending to the same page with query strings (?action=x) so that when it sends to the same page at the beginning of the page it will see that _GET[action] isset and perform the desired action.

FOR EXAMPLE: Say you want to allow a user to delete a comment made at the end of one of their posts (on a blog). The way I learned to do it is to make the delete button a link to "currentpage.php?action=delete". Clicking the button will in a sense refresh the page where the page will see that _GET[action]=="delete" and run the code to delete the comment.

Is this the best way to do this? Is there not a way to make a click of a button cause code to run (in php) without having to refresh the page?

In addition to causing the user to lose their place in the page, there are certain cases when I won't know the actual page url and need to reference it (like if you have a "comments" function in a file that's required on each page so that you don't have to use the code to display the comments on every single page that has comments). So, I'd need a way to reference the current page in the link since I won't know it ahead of time. Is there a way to do that?

I apologize for not being clear enough earlier. Is this any better?

Jonathan Grant
  • 143
  • 2
  • 15
  • 2
    Could you be a bit more clear please? Posting some of your code would help. – Matthew Herbst Apr 30 '15 at 21:23
  • specify just the filename maybe along with the get parameter? `anotherpage.php?action=youraction` . This will resolve correctly if all the pages are in same directory. – Maximus2012 Apr 30 '15 at 21:24
  • *"Also, I'd like it if it went back to the same spot in the page instead of the very top of the page."* - Can't be done in PHP, you'll need to use a JS solution. – Funk Forty Niner May 01 '15 at 01:10

1 Answers1

-1

Take a look at the $_SERVER superglobal. It contains all the information you'll need about the current request, to reconstruct it.

echo '<pre>'; print_r($_SERVER); echo '</pre>';

John Cartwright
  • 5,109
  • 22
  • 25
  • Not sure why I received a down vote for this. – John Cartwright Apr 30 '15 at 21:32
  • I think this more suited as a comment rather than an answer. In this case it wasn't even clear what the OP was trying to ask. If OP understood the basics of PHP/Sessions/HTTP-GET/POST then the question would not have been needed even. – Maximus2012 May 01 '15 at 13:21
  • I understand the basics, I think. But that's about it. lol I'm not sure what isn't understood by the question. – Jonathan Grant May 01 '15 at 19:36
  • $_SERVER['PHP_SELF'] works to show the current file name which is the answer to the second part of my question. Thanks so much for steering me to the _SERVER superglobal! :) – Jonathan Grant May 01 '15 at 19:59