html - php 获取表单数据
我有两个php问件
其中
add.php:
<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:
<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>
提交数据时为什么会报如下错误:
<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:
<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:
<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>
提交数据时为什么会报如下错误:
<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);

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

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

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

PHP用于构建动态网站,其核心功能包括:1.生成动态内容,通过与数据库对接实时生成网页;2.处理用户交互和表单提交,验证输入并响应操作;3.管理会话和用户认证,提供个性化体验;4.优化性能和遵循最佳实践,提升网站效率和安全性。

HTML、CSS和JavaScript在Web开发中的作用分别是:1.HTML定义网页结构,2.CSS控制网页样式,3.JavaScript添加动态行为。它们共同构建了现代网站的框架、美观和交互性。

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

PHP的核心优势包括易于学习、强大的web开发支持、丰富的库和框架、高性能和可扩展性、跨平台兼容性以及成本效益高。1)易于学习和使用,适合初学者;2)与web服务器集成好,支持多种数据库;3)拥有如Laravel等强大框架;4)通过优化可实现高性能;5)支持多种操作系统;6)开源,降低开发成本。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
