If you read something about header in php functions, you will came to know that nothing gets printed after header as you are giving instructions to redirect on index.php
However to achieve the same, you can use session variables.
So, on the page where you are redirecting to index.php:
$_SESSION['error']=true;
header('location: index.php');
On index.php, check if session variable is true:
if($_SESSION['error']== true)
{
echo '<script type="text/javascript">
document.getElementById("nick").value = "invalid email";
document.getElementById("nick").className = "invalidemail";
</script>';
}
solved Echo after header