0

trying to create a chat and keep getting Undefined index i've tried adding ? $_POST['chat'] : null; but doesn't work

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

Line 8:

$sent = $_POST['chat'];

the variable is used here:

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>

Variables:

    $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";

Edit: This is not a duplicate because this is for a very specific piece of code, i've also already used empty and I would not like to ignore the problem because it is a possible cause of another problem.

Thank you.

jane boe
  • 23
  • 1
  • 4
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Narendrasingh Sisodia Apr 30 '16 at 05:26
  • Refer to http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770836#12770836 – Julie Pelletier Apr 30 '16 at 05:32

4 Answers4

1

You said you tried a ternary conditional, but didn't post an example of what you tried. It should look like this:

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

In PHP 7.0 or higher, you can simplify this expression with the null coalesce operator:

$sent = $_POST['chat'] ?? null;
Dave Ross
  • 3,313
  • 1
  • 24
  • 21
1

Use this code :

<?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 />';
    }
}
?>
Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20
0

did you submit the form ?

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>
SonDang
  • 1,468
  • 1
  • 15
  • 21
  • the code works fine in my environment, can you post your html code to the question? Also, try with another name beside chat. – SonDang Apr 30 '16 at 05:45
0

Do something like $sent = isset($_POST['chat']) ? $_POST['chat'] : '';

btw.: There is much redundance in you code.

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

If you don't want to write an isset() condition every time, you could define a short function:

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

Use it like that:

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

or just

$sent = get($_POST['chat']);
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42