0

I am trying to generate an error message in my HTML if one of these two things are happening, however the appropriate message is displaying the HTML, but I'm also given a warning for having a undefined variable.

How would I fix it with how I am doing it?

Code:

$tweet = "{$_POST['tweet']}";

$errorOne = "Error: Your tweet must be less than 140 characters.";
$errorTwo = "Error: Your tweet must not be blank.";

if(strlen($tweet) > 140){
    $errorOneOutput = $errorOne;
}elseif(empty($tweet)){
    $errorTwoOutput = $errorTwo;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
</head>
<body>
    <?= $errorOneOutput ?>
    <?= $errorTwoOutput ?>
</body>
</html>

I have tried using exit() in my PHP block instead of embedded HTML but then that doesn't generate the HTML. I specifically need the error message to be displayed within the HTML like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Challenge 3</title>
</head>
<body>
    <br />
<b>Warning</b>:  Undefined variable $errorOneOutput in <b>C:\xampp\htdocs\WEBD\Challenges\Challenge 3 - Twitter Challenge\insert.php</b> on line <b>29</b><br />
    Error: Your tweet must not be blank.</body>
</html>

I also know that I can just echo out my errors, but again those would be above the HTML and not within it.

3 Answers3

0

Put ‘$errorOneOutput = null’ at the top to make sure the variable is always defined.

Simon Epskamp
  • 8,813
  • 3
  • 53
  • 58
0

The issue is you're trying to print a variable you haven't initialised:

<?= $errorOneOutput ?>
<?= $errorTwoOutput ?>

To initialise these variables, add this to the top of your php file:

$errorOneOutput = "";
$errorTwoOutput = "";
Joundill
  • 6,828
  • 12
  • 36
  • 50
0

$errorOneOutput is only ever defined if the length of the string contained in $tweet is greater than 140 characters. Also, there is no point re-assigning the same value to duplicate variables. You should consider refactoring your code as follows:

<?php
$tweet = $_POST['tweet'];

$errorOne = "Error: Your tweet must be less than 140 characters.";
$errorTwo = "Error: Your tweet must not be blank.";
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
</head>
<body>
    <?php if (strlen($tweet) > 140) : ?>
        <?= $errorOneOutput ?>
    <?php elseif (empty($tweet)) : ?>
        <?= $errorTwoOutput ?>
    <?php endif ?>
</body>
</html>
BenM
  • 52,573
  • 26
  • 113
  • 168
  • This is what I had originally, however it was specified in my instructions that I needed to have all my PHP within the PHP block at the start, not embedded in the HTML. Kind of strange, I know, but that is what was specified. Thank you though! –  Jan 20 '22 at 21:39
  • @blacksheep01 You don't have all of your PHP in the block at the start anyway, you're using short tags to echo output: `= $errorOneOutput ?>`. – BenM Jan 20 '22 at 21:40