未定义的索引:在第4行输入C:\ wamp \ www \ submit.php

时间:2012-04-12 11:01:58

标签: php undefined

嗨我有未定义索引的问题。 在谷歌C​​hrome和Mozilla它显示通知但一切正常在资源管理器中它不起作用。 我的PHP版本是5.3.5。

代码:HTML文件

<div id="stylized" class="myform">
    <form action="submit.php" method="post" id="Form">
        <ol>
            <label for="number">number:</label>
            <input name="number" class="text" type="text" size="28"/>
            <div id="butt">
                <input type="hidden" style="width: 150px; border: 1px solid gray" value="addd" name="type" />
                <input type="submit" class="formbutton" value="add" />
            </div>
            <div id="error">&nbsp;</div>
        </ol>
        <div class="spacer"></div>
    </form>
</div>

提交文件submit.php

switch ($_POST['type']){
    case "register":
        register();
        break;
    case "login":
        login();
        break;
    default:
        die(msg(0, "error"));
}

错误:

Notice: Undefined index: type in C:\wamp\www\submit.php on line 4

我已经尝试过了:

if (isset($_POST['type']))
 $type = $_POST['type'];

以及在其他主题上发现的许多其他建议,但在这种情况下似乎没有帮助

5 个答案:

答案 0 :(得分:0)

您没有名为“type”的输入?尝试将$ _POST ['type']更改为$ _POST ['number'],如果这是您需要的数据。

答案 1 :(得分:0)

我猜你在提交上述表格之前通过其他途径进入submit.php内部。

试试这个:

switch (isset($_POST['type']) ? $_POST['type'] : NULL) {

或者替代方案,通常是避免但可能是好的方法:

switch (@$_POST['type']) {

答案 2 :(得分:0)

$_REQUEST['type'] = ( isset($_REQUEST['type']) ? $_REQUEST['type'] : '' )

检查变量是否已设置 - 如果没有,请将其设置为空白,尽管我会检查您的表单,以确定未设置帖子字段

答案 3 :(得分:0)

您可以在此行跳过此错误

`ini_set("display_errors", "off");`

当您使用会话或数组但没有解释索引时,将返回此错误, 要解决此问题,您需要添加空值,或者如果尚未使用此索引,则需要添加任何内容。

答案 4 :(得分:-1)

你的

<input type="hidden" style="width: 150px; border: 1px solid gray" value="addd" name="type"/>

还应该有id="type"

编辑:

BTW,刚才提到,这个inputhidden。也许您的switch应该基于另一个变量,例如$_POST['number']或其他文字字段?