William
William

Reputation: 11

How do you get a PHP page with arguments to point to itself?

I'm trying to make a page where, when a form is submitted, it returns to the page from where the form was called from. Normally, I'd just set the form action to basename($_SERVER['SCRIPT_NAME']); and it would work fine. The problem that I'm having is that the form on this page is now being called from a url like www.yaddayadda.com/article.php?id=4, so when I use $_SERVER['SCRIPT_NAME'] it only returns article.php. Is there anyway to make it return the variables after the script name as well?

Upvotes: 1

Views: 74

Answers (3)

Álvaro González
Álvaro González

Reputation: 146460

You probably need $_SERVER['REQUEST_URI']. If you print_r() all $_SERVER variables, you'll see there're several good candidates. The main difference comes when your script goes through CGI, gateways or URL rewriting (such as Apache's mod_rewrite). I believe REQUEST_URI is pretty safe.

Upvotes: 1

neo
neo

Reputation: 1270

Use $_SERVER['SCRIPT_NAME'].$_SERVER['QUERY_STRING'] or $_SERVER['REQUEST_URI'] as stated here.

Upvotes: 0

Pekka
Pekka

Reputation: 449515

You can add $_SERVER["QUERY_STRING"] or use $_SERVER["REQUEST_URI"] that combines both.

It's often useful to do a print_r($_SERVER); or phpinfo(); to find out what your specific environment has to offer in terms of server variables.

Upvotes: 0

Related Questions