$ _POST未定义索引PHP错误 [英] $_POST undefined index PHP error

查看:163
本文介绍了$ _POST未定义索引PHP错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接收字符串的我的html代码:

 <!DOCTYPE html> 
< head>
< title> title< / title>
< / head>
< body>
< font face =Segoe UIsiz =3>
< form action =process.phpaction =post>
建议:< input type =textname =sgstautocomplete =off>
< input type =submit>
< / form>
< / font>
< / body>
< / html>

我的PHP处理它:(process.php)

 <?php 
if($ _POST [sgst] ==john){
echojohn;
} else {
echosomeone;
}
?>

我收到错误


注意:未定义的索引:第2行的C:\ xampp \\\ htdocs \ test \ process.php中的sgst


谁能告诉我导致错误的原因是什么?

解决方案

你正在重复使用<$ c 表单中的$ c> action 属性:

 < form action =process.phpaction =post> 

我很惊讶表单甚至在发布时都是这样。尽管有这些症状,但似乎至少足以混淆POST。我想你应该使用方法属性:

 < form action =process.phpmethod =post> 

其他注意事项:


  • 您需要在服务器端代码中使用 isset()。该代码不应该假设所有值都将被公布,因为服务器无法控制客户端发送的内容。最好在使用它之前明确验证数据。

  • font 元素是相当过时的,如果它们没有正式弃用他们应该是:) CSS样式将成为客户端UI风格的方式。


My html code which receives the string:

<!DOCTYPE html>
<head>
    <title>title</title>
</head>
<body>
    <font face="Segoe UI" siz ="3">
    <form action="process.php" action="post">
    Suggestion: <input type="text" name="sgst" autocomplete="off">
    <input type="submit">
</form>
</font>
</body>
</html>

My PHP which processes it: (process.php)

<?php
    if ($_POST["sgst"]=="john") {
        echo "john";
    } else {
        echo "someone";
    }
?>

I am getting the error

Notice: Undefined index: sgst in C:\xampp\htdocs\test\process.php on line 2

Can anyone tell me what is causing the error?

解决方案

You're re-using the action attribute on the form element:

<form action="process.php" action="post">

I'm surprised the form is even posting at all given that. Though given the symptoms it seems to at least be enough to confuse the POST. I think you meant to use the method attribute:

<form action="process.php" method="post">

A couple other notes:

  • You'll want to make use of isset() in your server-side code. The code shouldn't assume that all values will be posted, since the server can't control what a client sends it. Best to explicitly validate the data coming in before using it.
  • font elements are pretty dated, and if they're not officially deprecated they should be :) CSS styling would be the way to go for client-side UI styles.

这篇关于$ _POST未定义索引PHP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆