带有 isset $_POST 的 PHP 未定义索引

php undefined index with isset $_POST

提问人:jane boe 提问时间:4/30/2016 最后编辑:jane boe 更新时间:4/30/2016 访问量:3114

问:

尝试创建聊天并继续获取我尝试添加的未定义索引,但不起作用? $_POST['chat'] : null;

通知:Undefined index: chat in /Applications/MAMP/htdocs/chat/chat.php on line 8

第 8 行:

$sent = $_POST['chat'];

此处使用该变量:

if (isset($_POST['chat'])) {
    if (!empty($sent)) {
        fwrite($myfile, $first.': '.$txt.'=');
        fclose($myfile);
    } else if (empty($sent)) {
        if(isset($_POST['chat'])){
            echo 'Cant send an empty message','<br />';
        }
    }
}

HTML格式:

<body>
    <iframe id='reload' src='refresh.php'>
        <fieldset class="field">
                <div id="list"><p><?php
                    $filename = 'chat.txt';
                    $handle = fopen($filename, 'r');

                    $detain = fread($handle, filesize($filename));

                    $chat_array = explode('=', $detain);

                    foreach($chat_array as $chat) {
                        echo $chat.'<br />';
                    }
                    ?></p></div>
        </fieldset>
    </iframe>
    <form action="chat.php" method="POST">
        <input type="text" name="chat" class="textbox">
        <input type="submit" value="Send" class="button">
    </form>
</body>

变量:

    $sent = $_POST['chat'];
    $myfile = fopen("chat.txt", 'a') or die("Unable to open file!");
    $txt = ($sent."\n");
    $first = getuserfield('username');
    $active = ($first.":".$ip_addr);
    $activef = fopen("ip-user.txt", 'a');
    $myFile = "domains/domain_list.txt";

编辑:这不是重复的,因为这是针对一段非常具体的代码,我也已经使用了空,我不想忽视这个问题,因为它可能是另一个问题的原因。

谢谢。

php isset 未定义索引

评论

2赞 Narendrasingh Sisodia 4/30/2016
PHP 的可能重复:“注意:未定义的变量”和“注意:未定义的索引”
0赞 Julie Pelletier 4/30/2016
请参阅 stackoverflow.com/questions/12769982/...

答:

0赞 SonDang 4/30/2016 #1

你提交了表格吗?

PHP格式:

if (isset($_POST['chat'])) {
if (!empty($sent)) {
    fwrite($myfile, $first.': '.$txt.'=');
    fclose($myfile);
} else if (empty($sent)) {
    if(isset($_POST['chat'])){
        echo 'Cant send an empty message','<br />';
    }
}

}

HTML格式:

<form action="" method="POST">
    <input type="text" name="chat">
    <input type="submit" name="submit">
</form>

评论

0赞 SonDang 4/30/2016
该代码在我的环境中运行良好,您可以将您的 HTML 代码发布到问题中吗?另外,尝试在聊天旁边使用另一个名字。
1赞 Dave Ross 4/30/2016 #2

你说你尝试了三元条件,但没有发布你尝试过的示例。它应如下所示:

$sent = isset($_POST['chat']) ? $_POST['chat'] : null;

在 PHP 7.0 或更高版本中,您可以使用 null 合并运算符简化此表达式:

$sent = $_POST['chat'] ?? null;
1赞 Manjeet Barnala 4/30/2016 #3

使用以下代码:

<?php 
$sent = '';
if(isset($_POST['chat'])) 
{
    $sent = $_POST['chat'];
    if (!empty($sent))
    {
        $txt = ($sent."\n");
        fwrite($myfile, $first.': '.$txt.'=');
        fclose($myfile);
    } 
    else 
    {
        echo 'Cant send an empty message','<br />';
    }
}
?>

评论

0赞 jane boe 4/30/2016
这将删除错误,但随后不会提交任何文本
0赞 Manjeet Barnala 4/30/2016
你从哪里来??$txt
0赞 jane boe 4/30/2016
文字来自$sent
0赞 Manjeet Barnala 4/30/2016
在非空状态下使用$txt = ($sent."\n");$send
0赞 Manjeet Barnala 4/30/2016
伟大!!。。感谢您的赞赏..@janeboe
0赞 Pinke Helga 4/30/2016 #4

做类似的事情$sent = isset($_POST['chat']) ? $_POST['chat'] : '';

顺便说一句:你的代码中有很多冗余。

if (isset($_POST['chat'])) {
  if (!empty($sent)) {
    fwrite($myfile, $first.': '.$txt.'=');
    fclose($myfile);
  } else {
    echo 'Cant send an empty message','<br />';
  }
}

如果你不想每次都写一个条件,你可以定义一个短函数:isset()

function get(&$var, $default = null)
{
  return isset($var) ? $var : $default;
}

这样使用它:

$sent = get($_POST['chat'], '');

或者只是

$sent = get($_POST['chat']);