You may have the action value that you'd like, as the following HTML indicates:
<form name="myform" action="testform.php?query=string" method="POST">
<input name="data" type="text">
<input type="submit">
</form>
The way your form is currently set up, by default, it results in a GET request. However, your PHP tests for a form submitted as a POST request! If you really intend to submit the form as a POST request then you must specify that in the action attribute as action="POST"
. Then, change your test of the submitted form to check whether the POST has a value or not, as follows:
<?php
if (isset($_POST) && $_POST != null){
echo ( htmlentities( $_SERVER['QUERY_STRING'] ) ); // adding more security
}
?>
Note: when the method is POST, the portion ?query=string
does not reflect a GET variable but rather a QUERY_STRING. When this form is submitted it redirects to testform.php?query=string
.
Incidentally, a handy function for parsing the resulting query string is parse_str and you can use it to create a variable or an associative array that contains the "string" value, as follows:
<?php
$qs = htmlentities( $_SERVER['QUERY_STRING'] );
$str = substr( $qs, 1 ); // remove the '?'
// array option
parse_str( $str ,$arr);
echo $arr['query'],"\n";
// variable option
parse_str( $str );
echo $query;
See http://3v4l.org/DC27Y