登录

html - php 获取表单数据

我有两个php问件
其中
add.php:

<form action="action.php?action=add" method="post">
    <table>
        <tr>
            <td>姓名</td>
            <td>
                <input type="text" name="name"/>
            </td>
        </tr>
        <tr>
            <td>性别</td>
            <td>
                <input type="radio" name="sex" value="w"/>男
                <input type="radio" name="sex" value="n"/>女
            </td>
        </tr>
        <tr>
            <td>年龄</td>
            <td>
                <input type="text" name="arge"/>
            </td>
        </tr>
        <tr>
            <td>班级</td>
            <td>
                <input type="text" name="classId"/>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
                <input type="submit" value="提交"/>
                <input type="reset" value="重置"/>
            </td>
        </tr>
    </table>
</form>

action.php:

switch($_GET['action']){
    case "add":
        $name = $_POST['name'];
        $sex = $_POST['sex'];
        $arge = $_POST['arge'];
        $classId = $_POST['classId'];
        $sql = "insert into students VALUES (NULL ,'{$name}','{$sex}','{$arge}','{$classId}')";
        $rw = $pdo->exec($sql);
        if($rw>0){
            echo "<script>alert('添加成功');window.location='index.php'</script>";
        }else{
            echo "<script>alert('添加失败');window.history.back();</script>";
        }
    break;
}

提交数据时为什么会报如下错误:

Notice: Undefined index: name in /Applications/XAMPP/xamppfiles/htdocs/studentsManager/action.php on line 16

Notice: Undefined index: sex in /Applications/XAMPP/xamppfiles/htdocs/studentsManager/action.php on line 17

Notice: Undefined index: arge in /Applications/XAMPP/xamppfiles/htdocs/studentsManager/action.php on line 18

Notice: Undefined index: classId in /Applications/XAMPP/xamppfiles/htdocs/studentsManager/action.php on line 19

name sex arge classId 不是都有么 为何还时 未定义??

# PHP
怪我咯怪我咯2219 天前572 次浏览

全部回复(3) 我要回复

  • PHP中文网

    PHP中文网2017-04-10 16:40:41

    • -问题没发现,倒是发现第一次看到case可以这样用的,就不怕出错么?

    回复
    0
  • PHP中文网

    PHP中文网2017-04-10 16:40:41

    form 没有设置encoding 属性。

    encoding='application/x-www-form-urlencoded'

    回复
    0
  • PHP中文网

    PHP中文网2017-04-10 16:40:41

    这样吧。你先var_dump($_POST);

    回复
    0
  • 取消回复发送