I'm using the following PHP code, on a page that a user reach on after submitting a form with a certain foo
input name in it, which this PHP code processes, and decides to which URL that user be forwarded accordingly.
I've just noticed that If a user does not enter that page with an foo
input name (for example, let's say the form on the previous page had a `vvv' as the input name instead, due to some error),
then this PHP code would not send the user to the default URL. instead, it would refresh itself every 3 seconds in a loop.
Why? shouldn't the default value be obtained in case of any error, including the above scenario?
CODE:
<?php
if(isset($_POST['foo'])){
switch ($_POST['foo']) {
case "aaa":
$url = "http://www.aaa.com/";
break;
default:
$url = "http://www.bbb.com/";
}
}
header( "refresh:3;url=$url" );
?>
<!doctype html>
<html>
<head>
<style>
.test {display: block;}
</style>
</head>
<body>
test
</body>
</html>
Please verify that the
switch
snipet is reached when this page is navigated to withoutfoo
parameters.In your case, the
$url
has not been initiated yet (may be theif
condition was failed). So that theheader
function will actually send the browser this header:refresh:3;url=
(empty string afterurl=
), which cause the browser refresh with current url every 3 seconds.1) I think you are messing with
$_POST
and$_GET
.Get
params are visible in URL,POST
is not shown in url.2) If there is no
$_POST['foo']
, than it will throw error, because there is no set$url
variable. Better change to this code:It would be better if you set the default url at the top. So even if none of the case get match. Default value will always be there. If the value in case gets matched, then it will be replaced.
Does
$_POST['foo']
has any value at all? Try dump the value to check if there is anything in it:If the result of this is null then there is an error in your form.
Something like this should work:
And the action: