0

I have a PHP file which, when run produces a white screen and this error: "The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol." Looking at other answers on this site the most common answer is to add

<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">

in the <head>. However I have this and am still getting the error. I have also tried

header('Content-type: text/html; charset=utf-8');

in the PHP and it still doesnt work. When I remove all my php code it works, anyone know what I might have in my code to cause this error? Im a bit of a novice to PHP and still dont really know what im doing. my full php code is so:

$favisit = $favanimal = $age = $area = "";
$viserror = $anierror = $ageerror = $areaerror = "";
$errormessage = $extra = "";
$errorlist = [$viserror,$anierror,$ageerror,$areaerror]
$counter = 0;

if ($_SERVER["REQUEST_METHOD"]=="POST") {
    if (empty($_POST["favisit"])) {
        $viserror = " the favorite bit of your visit";
    } else {
        $favisit = test_input($_POST["favisit"]);
    }
    if (empty($_POST["favanimal"])) {
        $anierror = " your favorite animal";
    } else {
        $favanimal = test_input($_POST["favanimal"]);
    }
    if (empty($_POST["age"])) {
        $ageerror = " your age";
    } else {
        $age = test_input($_POST["age"]);
    }
    if (empty($_POST["area"])) {
        $areaerror = " the area where you live";
    } else {
        $area = test_input($_POST["area"]);
    }
}

foreach($errormessage as $error) {
    if ($error == "") {
        $counter = $counter+1
    }
}
if ($counter != 0) {
    $errormessage = "You have not filled out"
    if ($counter == 1) {
        foreach($errormessage as $error) {
            if ($error == "") {
                $errormessage = $errormessage . $error . ".";
            }
        }
    } else {
        $extra = ","
        foreach($errormessage as $error) {
            if ($error == "") {
                if ($counter == 1) {
                    $extra = " or"
                }
                $errormessage = $errormessage + $extra + $error + ".";
                $counter = $counter -1;
            }
        }
    }
}

Again I'm still a novice to PHP so its probably a stupid mistake I cant pick up on. Thanks in advance!

Dharman
  • 21,838
  • 18
  • 57
  • 107
haco
  • 23
  • 5
  • Try this : – Inazo Aug 24 '18 at 14:38
  • `$errormessage = $errormessage + $extra + $error + ".";` should use `.`s not `+`s. – user3783243 Aug 24 '18 at 14:38
  • That error *sounds* like it comes from [WebPack DevServer](https://webpack.js.org/configuration/dev-server/) ... it's not a standard PHP error message. I'm guessing you've got a PHP error, with display errors **on**, using that *DevServer* ... and since PHP will spaff out the error message as an HTML snippet (by default) you're getting that encoding message from the DevServer. – CD001 Aug 24 '18 at 14:41
  • ensure that the header is set before you write anything into the html buffer. (eg: pure html or echo), ensure that your apache server doesn't change the header (by looking in http header (in chrome F12 and Network tab and refresh)) – Jimbot Aug 24 '18 at 14:42
  • You should do two important things. Use an IDE that will highlight mistakes and Turn on your PHP display error. If you're new to php, most developers have these two things covered. =) – unixmiah Aug 24 '18 at 14:46

1 Answers1

3

You have multiple PHP errors. E.G missing ; on several lines. So your request probably returns a 500, and you're getting an error in your error log.

This means you are getting an empty body (and obviously that does not have any meta declaration in it. The error is true but misleading)

you can probably benifit from How do I get PHP errors to display?

examples:

  • $counter = $counter+1 needs a ;
  • $errormessage = "You have not filled out" needs a ;
  • $errormessage = $errormessage + $extra + $error + "."; should be concatenated with ., not +
  • $extra = "," needs a ;
  • $extra = " or" needs a ;
Nanne
  • 61,952
  • 16
  • 112
  • 157
  • 1
    Using a proper code editor with syntax highlighting and linter would certainly help you out too. – Dharman Aug 24 '18 at 14:42
  • yeah, most definitely! – Nanne Aug 24 '18 at 14:43
  • The program I use to code in usually fills in the ; and so i have gotten into lazy habits. Just to make sure I put them in and it worked so thankyou – haco Aug 24 '18 at 14:55