简体   繁体   中英

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

Hi I have problem with undefined index. in google chrome and mozilla it shows the notice but everything works fine in explorer it doesnt work. my PHP version is 5.3.5.

The code: HTML file

<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 FILE submit.php

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

The error:

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

I already have tried:

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

and many other suggestions found on other topics but it doesnt seem to be helping in this case

You don't have an input named "type"? Try changing $_POST['type'] to $_POST['number'], if that is the data, you need.

I'm guessing you're ending up inside submit.php by some other route than submitting the above form.

Try this:

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

Or the alternative, generally-to-be-avoided-but-probably-OK-here approach:

switch (@$_POST['type']) {
$_REQUEST['type'] = ( isset($_REQUEST['type']) ? $_REQUEST['type'] : '' )

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

you can skip this error with this line

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

this error returns when you use session or array but there is an index is not explained yet, to fix this you need to add null value or what ever you like if this index is not used yet.

Your

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

Should have also id="type" .

EDIT:

BTW, just mentioned, this input is hidden . Maybe your switch should be based on another variable, for example $_POST['number'] or other text field?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM