0

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";
}
1
  • 1
    afaik you can't echo anything after you've set header with location. Commented Apr 2, 2012 at 10:16

2 Answers 2

5

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'];
Sign up to request clarification or add additional context in comments.

Comments

2

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...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.