PHP switch-case语句中的默认情况并不真正作为默认值? [英] default case in a PHP switch-case statement doesn't really work as the default?

查看:148
本文介绍了PHP switch-case语句中的默认情况并不真正作为默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下PHP代码,在用户提交的页面上提交了一个带有某个 foo 输入名称的表单,这个PHP代码进程,并决定相应转发用户的URL。

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.

我刚注意到,如果用户没有输入带的页面foo 输入名称(例如,假设前一页上的表单有'vvv'作为输入名称,而不是因为某些错误),
那么这个PHP代码就不会发送用户使用默认URL。相反,它会在循环中每隔3秒刷新一次。

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?

代码:

<?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>


推荐答案

$ _ POST [' foo'] 有什么价值吗?
尝试转储该值以检查其中是否有任何内容:

Does $_POST['foo'] has any value at all? Try dump the value to check if there is anything in it:

var_dump($_POST['foo'])

如果结果为null,则表单中有错误。

If the result of this is null then there is an error in your form.

这样的事情应该有效:

<form method='post' action = $this->url()>
  <div>
    <input type='text' name='foo' value='foo'>
  </div>
  <div>
    <input type='submit' value='submit' name='submit'>
  </div>
</form>

行动:

if(isset($_POST['foo']) && !empty($_POST['foo'])){

    switch ($_POST['foo']) {

    case "aaa":
        $url = "http://www.aaa.com/";
        break;

    default:
        $url = "http://www.bbb.com/";
    }
}

这篇关于PHP switch-case语句中的默认情况并不真正作为默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆