Reputation: 9838
Im trying to update the contents of an element after running some php code. I realize the php is executed first, but I thought by loading the page I could then find the element? However console says cannot find element of null so I guess the page isn't loading before the innerHTML code is running.
Anyone any ideas?
else if(strlen($_POST['username']) < 6){
header("Location: http://webpage/register.html");
echo "document.getElementById('elemID').innerHTML = usename too short";
}
Upvotes: 0
Views: 259
Reputation: 12018
First, you shouldn't really have any logic after your header Location redirect. It is good practice to put "exit" or "die" after a redirect like that as you can't guarantee that the browser will ever see the next line before redirecting. In fact, you can pretty well guarantee that it will more often not see that code.
If you're going to redirect, put your error as an argument to your redirect URL and have logic there that shows the error like this:
header("Location: http://webpage/register.php?error=username%20too%20short");
Then in you register.php (I renamed it from .html so you can read the error argument) you can reference your error like:
$error = $_GET['error'];
if (!empty($error)) {
//write your error out in some markup or javascript...
}
Upvotes: 2
Reputation: 60526
header()
instructs your clients to go to the new location, hence outputting anything after that would make no effect to your client as the content of register.html is already handled differently by your server.
If you can change register.html to use php instead, you could pass
header("Location: http://webpage/register.php?msg=username%20too%20short");
Then in your register.php
if(!empty($_GET['msg'])) echo $_GET['msg'];
Upvotes: 5