目录
回复内容:
首页 后端开发 php教程 html - php 获取表单数据

html - php 获取表单数据

Jun 06, 2016 pm 08:20 PM
html php

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

<code><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> </td>

            <td>

                <input type="submit" value="提交">

                <input type="reset" value="重置">

            </td>

        </tr>

    </table>

</form></code>

登录后复制
登录后复制

action.php:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<code>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;

}</code>

登录后复制
登录后复制

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

1

2

3

4

5

6

7

8

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

</code>

登录后复制
登录后复制

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

回复内容:

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

<code><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> </td>

            <td>

                <input type="submit" value="提交">

                <input type="reset" value="重置">

            </td>

        </tr>

    </table>

</form></code>

登录后复制
登录后复制

action.php:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<code>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;

}</code>

登录后复制
登录后复制

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

1

2

3

4

5

6

7

8

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

</code>

登录后复制
登录后复制

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

你后面的错误是你 单独 在地址栏中打开 你的 action.php?action=add的时候显示的吧?
当你单独在浏览器中直接打开你的action.php?action=add的时候, 它是以GET的方式请求的, 而且不会有POST的数据过去, 所以你这个时候, 去取$_POST['xxx'] PHP 会有一个 Notice 的提示(在允许显示的情况下).

正常从你的 add.php 点按钮(有填写页数据的情况下), 是没有报你说的那个问题的.

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

form 没有设置encoding 属性。

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

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

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1665
14
CakePHP 教程
1424
52
Laravel 教程
1322
25
PHP教程
1270
29
C# 教程
1249
24
PHP和Python:比较两种流行的编程语言 PHP和Python:比较两种流行的编程语言 Apr 14, 2025 am 12:13 AM

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

PHP行动:现实世界中的示例和应用程序 PHP行动:现实世界中的示例和应用程序 Apr 14, 2025 am 12:19 AM

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

PHP:网络开发的关键语言 PHP:网络开发的关键语言 Apr 13, 2025 am 12:08 AM

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP的持久相关性:它还活着吗? PHP的持久相关性:它还活着吗? Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在现代编程领域中依然占据重要地位。1)PHP的简单易学和强大社区支持使其在Web开发中广泛应用;2)其灵活性和稳定性使其在处理Web表单、数据库操作和文件处理等方面表现出色;3)PHP不断进化和优化,适用于初学者和经验丰富的开发者。

PHP和Python:代码示例和比较 PHP和Python:代码示例和比较 Apr 15, 2025 am 12:07 AM

PHP和Python各有优劣,选择取决于项目需求和个人偏好。1.PHP适合快速开发和维护大型Web应用。2.Python在数据科学和机器学习领域占据主导地位。

PHP与其他语言:比较 PHP与其他语言:比较 Apr 13, 2025 am 12:19 AM

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python:解释了不同的范例 PHP和Python:解释了不同的范例 Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP:处理数据库和服务器端逻辑 PHP:处理数据库和服务器端逻辑 Apr 15, 2025 am 12:15 AM

PHP在数据库操作和服务器端逻辑处理中使用MySQLi和PDO扩展进行数据库交互,并通过会话管理等功能处理服务器端逻辑。1)使用MySQLi或PDO连接数据库,执行SQL查询。2)通过会话管理等功能处理HTTP请求和用户状态。3)使用事务确保数据库操作的原子性。4)防止SQL注入,使用异常处理和关闭连接来调试。5)通过索引和缓存优化性能,编写可读性高的代码并进行错误处理。

See all articles