0

I am trying to execute some simple JQuery commands in an echo statement from PHP after the user submits a Log In form.

The user is able to Log In with the correct credentials, however if they enter incorrect ones it should show the invalid credentials paragraph.

Here is the code:

<?php
    //PHP code for login in, working, not needed to show
    echo 'Testing
        <script>
            $( ".wrong" ).show( "slow" );
        </script>';
?>


<!DOCTYPE html>
<html>

<head>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>

<body class="login">

<form action="" method="post" class="login-form">
    <p class="wrong" style="display: none;">Invalid Credentials</p>
    <a class="link" href="#">Forgot your password?</a>

    <button type="submit" class="login-btn" name="submitLogin">Log in</button>
</form>

</body>

NOTE: The Testing value from the echo displays, yet the JQuery does not execute.

It is not possible to put the PHP code after the HTML as it creates a session for my login form to work.

4
  • 2
    echoing anything before <!doctype> and <html> is invalid html, also you would be trying to execute jQuery calls before it has been included. Commented Jun 13, 2015 at 12:50
  • I need to do this for my Login Form to work though, as it creates a Session. I have also tried including the JQuery include in the echo statement with no luck. How would I get around this? @PatrickEvans Commented Jun 13, 2015 at 12:51
  • ...and include your jquery call into a document.ready section otherwise it's searching an element not still present in the DOM Commented Jun 13, 2015 at 12:51
  • Your code snippet does not create a Session, your session_start() should be at the top yes, but you don't have to echo your <script> at the top Commented Jun 13, 2015 at 13:04

2 Answers 2

2
<?php
    echo 'Testing
        <script>
            $( ".wrong" ).show( "slow" );
        </script>';
?>

This does not create a session, and does not need to be at the top. What needs to be at the top is a session_start() call yes, but echoing out script html is not required at the top

For instance you could do

<?php
session_start();
if(attemptLogin()){
  //logged in path
} else {
  //wrong credentials
  $LoginError = true;
}
?>

<!doctype html>
<html>
<head>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <?php if($LoginError):?>
    <script>
        jQuery(document).ready(function(){
            $( ".wrong" ).show( "slow" );
        });
    </script>;
    <?php endif;?>
</head>
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! At last someone who actually reads the question properly. /rant @PatrickEvans
0

You need to allow your js files to be loaded first, then the DOM to be loaded and then you can use your code below just before the </body> tag and it will work.

<?php
    //PHP code for login in, working, not needed to show
    echo 'Testing
        <script>
            $( ".wrong" ).show( "slow" );
        </script>';
?>

1 Comment

This is not possible as it needs to be at the top of the page for the Login Form to function without any errors, due to creating a session. @jaycp

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.