Submitting PHP Forms With URLs Containing Strings


Submitting PHP Forms With URLs Containing Strings

When you submit forms with PHP, the URL may or may not contain query strings. URLS with query strings look like http://example.com/page.php?id=22&name=Betty. URLS with query strings can pose problems when you submit a form to itself. Below, shows typical code that is used to submit a form to itself and code which can be used with query strings in the url.

The code below would work fine if the url was simple and had no query string; such as example.com/page.php

Here is typical code to submit a form to itself:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

The code below would work fine if the url had a query string; such as example.com/page.php?id=2

Here are 2 code samples which can be used to submit a form to itself where the url the query string shown above:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];?>">

<form method="post" action="<?php echo $_SERVER['PHP_SELF']."?id=".$id ;?>">