-1

I've narrowed down the possible errors to be within an if statement. I'm trying to make two images appear within divs if my php file returns true after authenticating a user. There error comes when I try to add the image to the divs "b1" and "b2" with "innerHTML."

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>
   <title> Incident Center </title>
   <!--<link rel="stylesheet" href="http://web.njit.edu/~swp5/assignment/style/style.css">-->
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <script type="text/javascript">
function ajaxFunction(){
    var ajaxRequest;  

    if (window.XMLHttpRequest)
    {
    ajaxRequest=new XMLHttpRequest();
    }
    else
    {
    ajaxRequest=new ActiveXObject("Microsoft.XMLHTTP");
    }

    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            if(ajaxRequest.responseText=="true")
            {
            //ERROR HERE
            document.getElementById('b1').innerHTML ="<img src="http://web.njit.edu/~swp5/assignment2/view.png" width=50 height=50>";
            document.getElementById('b2').innerHTML ="<img src="http://web.njit.edu/~swp5/assignment2/view.png" width=50 height=50>";

            }

            else
            {
            document.getElementById('n1').innerHTML = "<h4>IT WAS FALSE</h4>";
            }
        }
    }
    var un = document.getElementById('un').value;
    var pw = document.getElementById('pw').value;
    var queryString = "?un=" + un + "&pw=" + pw;
    ajaxRequest.open("GET", "auth.php" + queryString, true);
    ajaxRequest.send(null); 
}

    </script>
</head>

<body>


<div class="header">
Incident Center 

</div>



<p>


<div class="t1">
<form name='myForm'>
username: <input type='text' id='un'> <br>
password: <input type='password' id='pw'> <br>

<input type='button' onclick='ajaxFunction()' value='Enter' />
</form>
</div>



</p>


<p>

<div id="n1">
<div id="b1"></div>
<div id="b2"></div>
</div>

</p>

</body>



</html>
0

3 Answers 3

1

You have double quotes in your innerHTML value.

document.getElementById('b1').innerHTML ="<img src="http://web.njit.edu/~swp5/assignment2/view.png" width=50 height=50>";
document.getElementById('b2').innerHTML ="<img src="http://web.njit.edu/~swp5/assignment2/view.png" width=50 height=50>";

Change it to

document.getElementById('b1').innerHTML ='<img src="http://web.njit.edu/~swp5/assignment2/view.png" width=50 height=50>';
document.getElementById('b2').innerHTML ='<img src="http://web.njit.edu/~swp5/assignment2/view.png" width=50 height=50>';

Note the use of single quotes wrapping the strings you're trying to insert as they use " inside.

Similarly you could escape the quotes inside if you wanted to stick to using just double quotes although it seems pointless in this case. The escape character in javascript is a backslash . To further understand where you've gone wrong you can read http://www.quirksmode.org/js/strings.html which covers the topic well.

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

Comments

1

Change:

document.getElementById('b1').innerHTML ="<img src="http://web.njit.edu/~swp5/assignment2/view.png" width=50 height=50>";
document.getElementById('b2').innerHTML ="<img src="http://web.njit.edu/~swp5/assignment2/view.png" width=50 height=50>";

To:

document.getElementById('b1').innerHTML ='<img src="http://web.njit.edu/~swp5/assignment2/view.png" width=50 height=50>';
document.getElementById('b2').innerHTML ='<img src="http://web.njit.edu/~swp5/assignment2/view.png" width=50 height=50>';

Comments

0

There's a problem with the quotes at this line

document.getElementById('b1').innerHTML ="<img src="http://web.njit.edu/~swp5/assignment2/view.png" width=50 height=50>";

Try replacing it with this:

document.getElementById('b1').innerHTML ='<img src="http://web.njit.edu/~swp5/assignment2/view.png" width=50 height=50>';

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.