Search code examples
phphtmlrefresh

php clear text printed to webpage


I have a html/php script which prints a text string directly to the page. However when I refresh the page that text is still there. I've googled this and check this forum too and tried various suggestions e.g.

<input autocomplete="off">

and

<body onload="document.FormName.reset();">

etc. None of them work.

The php code that prints the string is (if regex matches serial number then execute command else print error string):

if (preg_match($snregex, $sn, $matches)) {
  <command>
} else {
  echo "Invalid SN. Please re-enter!";
}

Should I print the string using a different way that can be cleared on refresh?


Solution

  • if (preg_match($snregex, $sn, $matches)) {
     <command>
    } else {
      //echo "Invalid SN. Please re-enter!";
     }
    

    try commenting the echo and check if the text is still there.

    if not, every time the else loop gets executed and prints the statement.

    solution:

    if(isset($_POST['submit']) && (preg_match($snregex, $sn, $matches))
    {
     <command>
    } else {
      echo "Invalid SN. Please re-enter!";
     }