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.

2

4 Answers 4

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 />';
    }
}
?>
Sign up to request clarification or add additional context in comments.

5 Comments

that removes the error but the then no text is submitted
where is your $txt coming from ??
the text is coming from $sent
use $txt = ($sent."\n"); inside not empty $send condition
great!!.. Thank You for appreciation.. @janeboe
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;

Comments

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>

1 Comment

the code works fine in my environment, can you post your html code to the question? Also, try with another name beside chat.
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']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.