0

Can someone tell me what is wrong with my code?

        $c =  '<div class="desc">';
    if( $_POST['login_post'] == '1') { 
    $username = $_POST['user_login'];
    $password = $_POST['password'];
        $message = log_in_user($username, $password);
    }
    display_message($message); 
    $c .= '<form name="loginform" id="loginform" method="post" action="">
                            <label>Username:</label>
                            <div><input type="text" name="user_login" id="user_username"  value="" size="20"  maxlength="40" tabindex="10" /></div>
                            </div>
                            <div class="group">
                            <label>Password:</label>
                            <div><input type="password" name="password" id="user_password"  value="" size="20"  maxlength="40" tabindex="20" /></div>
                            </div>
                             <input type="hidden" name="login_post" value="1"/>

                            <div class="group">
                            <label></label>
                            <div><input type="submit" name="submit" id="submit" class="bluebutton" value="Log In Now" /><span class="forgotlink"><a href="/forgot-password/">Forgot your password?</a></span></div>
                            </div> 
                </form>

                    </div>';
    $c = str_replace('%value%',5,$c);
    return $c;

That display_message($message); function is supposed to display the message inside the desc div. Instead, it is displaying above (outside) the desc div.

Am I concatenating PHP code incorrectly? If yes, can some one correct me? Thanks!

Update:

Here is my display_message function:

function display_message( $message = false ) {
    if( is_wp_error( $message ) ) {
        echo '<div class="errorMessage"><p>' . $message->get_error_message() . '</p></div>';    
    }
    elseif ($message != '') {
        echo '<div class="successMessage"><p>' . $message . '</p></div>';   
    }
}
2
  • 1
    Does display_message() return a value, or does it simply have an echo within the function itself? Commented Mar 24, 2012 at 21:54
  • @MarkBaker I updated my display_message function code in the question. Please check it Commented Mar 24, 2012 at 22:02

3 Answers 3

2

display_message() will print out the message instead of concatenating it. Try changing display_message($message); to $c .= htmlspecialchars($message); and it should end up inside the div as it is supposed to.

Edit: Since display_message does more than just display the message, it needs to be rewritten slightly;

function format_message( $message = false ) {
    if( is_wp_error( $message ) ) {
        return '<div class="errorMessage"><p>' . $message->get_error_message() . '</p></div>';    
    }
    elseif ($message != '') {
        return '<div class="successMessage"><p>' . $message . '</p></div>';   
    }
}

and the concatenation should instead be;

$c .= format_message($message);
Sign up to request clarification or add additional context in comments.

7 Comments

That is not going to work if display_message() prints or echoes something.
@jeroen Yes my display_message echoes messages. I updated the question with my display_message function
@user1091558 Updated my answer with the new info.
@JoachimIsaksson Thanks for your answer. That display message function also used by some other pages. But string concatenation requires only for my login page. If i change display_message function won't it affect other pages which uses that function?
@user1091558 That's why I made it into a new format_message() function so it wouldn't affect other pages. You could make display_message into just an echo format_message($message) if you decide to go this way and want to eliminate some code.
|
1

try loading the login result into another variable, then displaying it, after that concenating it to $message.

$c =  '<div class="desc">';
if( $_POST['login_post'] == '1') { 
$username = $_POST['user_login'];
$password = $_POST['password'];
    $login_message = log_in_user($username, $password);
}
display_message($login_message); 
$c .= $login_message . '<form name="loginform" id="loginform" method="post" action="">
                        <label>Username:</label>
                        <div><input type="text" name="user_login" id="user_username"  value="" size="20"  maxlength="40" tabindex="10" /></div>
                        </div>
                        <div class="group">
                        <label>Password:</label>
                        <div><input type="password" name="password" id="user_password"  value="" size="20"  maxlength="40" tabindex="20" /></div>
                        </div>
                         <input type="hidden" name="login_post" value="1"/>

                        <div class="group">
                        <label></label>
                        <div><input type="submit" name="submit" id="submit" class="bluebutton" value="Log In Now" /><span class="forgotlink"><a href="/forgot-password/">Forgot your password?</a></span></div>
                        </div> 
            </form>

                </div>';
$c = str_replace('%value%',5,$c);
return $c;

Comments

1

If your function display_message() echoes something, you need to use output buffering to capture the result and add it to your $c variable. Otherwise it gets echoed right away.

Edit: With output buffering:

ob_start();
display_message($message); 
$c .= ob_get_contents();            // adding output to the variable
ob_end_clean();

A better option would probably be to have display_message() return a string and add that string to $c.

Based on your edit, I would just change the function to:

function display_message( $message = false ) {
    if( is_wp_error( $message ) ) {
        return '<div class="errorMessage"><p>' . $message->get_error_message() . '</p></div>';    
    }
    elseif ($message != '') {
        return '<div class="successMessage"><p>' . $message . '</p></div>';   
    }
    return NULL;
}

and do:

$c .= display_message($message); 

2 Comments

Thanks for your answer jeroen. But that display message function also used by some other pages. But string concatenation requires only for my login page. If i change display_message function won't it affect other pages which uses that function?
@user1091558 Yes it would. In that case you would have to use output buffering to capture the result and avoid it gets echoed right away. I'll add a simple example.

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.