1
 <div id="error" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="modal_head">Activation</h4>
                </div>
                <div class="modal-body">


                    **<p id="error_record"> <?php echo $msg; ?> </p>**           \\line 1


                    <p class="text-warning"><small id="direction"></small></p>

                </div>
                <div class="modal-footer" align="center">
                    <button onclick="redirect()" type="button" class="btn btn-primary" >Okay</button>
                </div>
            </div>
        </div>
    </div>

I need to compare the string in the div with id "error_record" as follows :

<script>
        window.onload=function()
        {
            $("#error").modal('show');
        }
        function redirect(){
            var val=document.getElementById("error_record").innerHTML;      \\line2
            alert(val);                                                      \\line 3
            if(val=="Invalid Link"||val=="Invalid Link."||val=="Empty Link")
            {
                window.location="index.php";
            }
            if(val=="Activation Successful"||val=="Your Account is already activated.")
            {
                window.location="dashboard.php";
            }
        }
    </script>

But it always returns false in the comparison, in comment line 2.

The alert in comment line 3 gives the same string, But the comparison returns false.

Does it have something to do with the echo in line 1?

i have $msg="Empty Link";

3
  • what do you mean by "But it always returns false in the comparison, in comment line 1." Commented Dec 29, 2014 at 12:37
  • please read $msg="Empty Link"; The string ought to be same but the comparison doesnt work. Commented Dec 29, 2014 at 12:38
  • try to add this add this => val = val.trim() Commented Dec 29, 2014 at 12:40

2 Answers 2

1

You have whitespaces where i have written underscores <p id="error_record">_<?php echo $msg; ?>_</p>, that is why your strings are not equat. Either remove the whitespaces, or use .trim() fuction on your innerHTML to remove the whitespace.

Sign up to request clarification or add additional context in comments.

2 Comments

You mean all the white spaces??
no, only inside <p> tag ( the ones that i replaced with the underscores ) the whitespaces inside $msg are fine.
0

Instead of using (double equals) ==

use indexOf() != -1 as shown below code and let me know whether it works or not:

if(val.indexOf("Activation Successful")!=-1||val.indexOf("Your Account is already activated.")!=-1) {
    window.location="dashboard.php";
}

Comments

Your Answer

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