Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-14-2011, 03:38 PM   PM User | #1
sam9461
New Coder

 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
sam9461 is an unknown quantity at this point
Javascript error: undefined is null or not an object (help please!)

Hi, I’m a beginner of coding in PHP, MySQL and Javascript, and encountered a javascript error that already took me several days to deal with, but still unsuccessful.

I wrote codes for the cases that users forget their ID or password. There are two links (one for ID, another one for password) embedded in the Login page. Cap = 1, or 2 are set for ID and password respectively. When a user click the ID link, cap=1 will be transferred to a PHP page. The PHP page will ask the user to provide his email address to get their ID (However, the secret question and answer button will be hidden until the user’s email address is right). The user can only be allowed to try three times. The email address will be used to check the record of the MySQL database by Javascript and Ajax. If the email address matches the record of the database, the secret question and answer button will be changed to be visible to ask the user to provide his answer for the secret question, again, only three tries are allowed. If his answer is right, an email about his ID information will be transferred to his email address, the user is asked to check it out in his email box. Similarly, if the user wants to get his password, cap =2 will be transferred to the PHP page, ….

Now, when I run the programme in IE 8, an error on page notice come out, “undefined is null or not an object” in the line 24 ( var capp = capt.indexOf('password')

Please see my following coding, and give me some suggestions, thank you so much for your help.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

window.onload = initialwork;
var requ = false;
//set checkVal to be 1 or 2 to differentiate the email check and answer check
var checkVal = 1;
var capVal = 10;
var count1 = 0;
var count2 = 0;
var ccap = 0;

function initialwork() {
document.getElementById("sques").style.visibility = "hidden";
document.getElementById("yans").style.visibility = "hidden";
document.getElementById("secretans").style.visibility = "hidden";
document.getElementById("gobutton").style.visibility = "hidden";
document.getElementById("submitbutton").onclick = checkemail;
}

function checkemail() {
count1 = count1 + 1;
var capp = -10;
var mail = document.getElementById("email").value;
var capt = document.getElementById("caption").value;
var capp = capt.indexOf('password');
if (capp < 0) {
ccap = 1;
} else {
ccap = 2;
}
var url1 = "emailansjudeg-english.php?email=" + mail;
url1 = url1 + "&cap=" + ccap;
url1 = url1 + "&chkval=" + checkVal;
url1 = url1 + "&smr=" + Math.random();
var re = /^[\w\.-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9\.-]+$/;
var emcheck = document.getElementById("emailCheck");
var eal = document.getElementById("email");
if (count1 > 3) {
emcheck.innerHTML = "All three tries were wrong, no more try please!";
emcheck.className = "emailinfo";
eal.className = "emailappearance";
eal.focus();
exit;
} else {

if ((mail == "") || (!re.test(mail))) {
emcheck.innerHTML = "Your email is wrong!";
emcheck.className = "emailinfo";
eal.className = "emailappearance";
eal.focus();
} else {
makeRequest(url1);
}
}
}

function checkans() {
count2 = count2 + 1;
checkVal = 2;
var userAns = document.getElementById("secretans").value;
var ancheck = document.getElementById("ansCheck");
var uans = document.getElementById("secretans");
if (count2 > 3) {
ancheck.innerHTML = "All three tries were wrong, no more try please!";
ancheck.className = "ansinfo";
uans.className = "ansappearance";
uans.focus();
exit;
} else {

if (userAns == ans) {
var url2 = "emailansjudeg-english.php?chkval=" + checkVal;
url2 = url2 + "&smr=" + Math.random();
makeRequest(url2);
} else {
ancheck.innerHTML = "Sorry, your answer is wrong!";
ancheck.className = "ansinfo";
uans.className = "ansappearance";
uans.focus();
}
}

}

function makeRequest(url) {
if (window.XMLHttpRequest) {
requ = new XMLHttpRequest();
} else {
if (window.ActiveXObject) {
try {
requ = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}

if (requ) {
requ.onreadystatechange = showcontents;
requ.open("GET", url, true);
requ.send(null);
} else {
if (checkVal == 1) {
emcheck.innerHTML = "Sorry, an XMLHttpRequest cannot be created for some reason.";
} else {
ancheck.innerHTML = "Sorry, an XMLHttpRequest cannot be created for some reason.";
}
}

}

function showcontents() {
if (requ.readyState == 4) {
if (requ.status == 200) {
var outMsg = requ.responseText;
} else {
var outMsg = "There was a problem with the request "+ requ.status;
}


if (checkVal == 1) {
document.getElementById("emailCheck").innerHTML = outMsg;
document.getElementById("emailCheck").className = "emailinfo";
if (outMsg == "OK! please answer the following question!") {
document.getElementById("sques").style.visibility = "visible";
document.getElementById("yans").style.visibility = "visible";
document.getElementById("secretans").style.visibility = "visible";
document.getElementById("gobutton").style.visibility = "visible";
document.getElementById("gobutton").onclick = checkans;
}
} else {
var outQues = outMsg.indexOf('secretques');
var outAns = outMsg.indexOf('secretans');
if (outQues >= 0) {
var ques = outMsg.substr(10, outMsg.length);
document.getElementById("quesArea").innerHTML = ques;
document.getElementById("quesArea").className = "quesinfo";
} else if (outAns >= 0){
var ans = outMsg.substr(9, outMsg.length);
} else {
document.getElementById("ansCheck").innerHTML = outMsg;
document.getElementById("ansCheck").className = "ansinfo";
}
}

}
}
sam9461 is offline   Reply With Quote
Old 07-15-2011, 01:18 AM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
you are trying to search a string named "capt" for an occurance of 'password'... capt is never set and therefore does not exist and therefore cannot be searched... also i think you need to change 'password' to "password" (quotes) but I am not sure
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Old 07-15-2011, 02:20 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,570
Thanks: 62
Thanked 4,061 Times in 4,030 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ummm...Alykins, try reading the code again.

Code:
var capt = document.getElementById("caption").value;
var capp = capt.indexOf('password');
How can you say there is no variable named capt?????

UPDATE: Okay, I read Alykins' post again. He didn't say there's no such variable; he says that the variable is never "set". So maybe he indeed means that it's never set to any valid value.

Now, it is quite possible there is no element on the page with an id of "caption". And almost surely if that is the case then the error message makes sense.

Since sam doesn't show the HTML--shows only the JS code--we have no way of knowing.

Oh...In any case, JavaScript makes no distinction between strings enclosed in quotes ("password") and those enclosed in apostrophes ('password') so that's nothing to do with the problem.
Old Pedant is online now   Reply With Quote
Old 07-15-2011, 03:13 AM   PM User | #4
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
Quote:
Originally Posted by Old Pedant View Post
Ummm...Alykins, try reading the code again.

Code:
var capt = document.getElementById("caption").value;
var capp = capt.indexOf('password');
How can you say there is no variable named capt?????

UPDATE: Okay, I read Alykins' post again. He didn't say there's no such variable; he says that the variable is never "set". So maybe he indeed means that it's never set to any valid value.

Now, it is quite possible there is no element on the page with an id of "caption". And almost surely if that is the case then the error message makes sense.

Since sam doesn't show the HTML--shows only the JS code--we have no way of knowing.

Oh...In any case, JavaScript makes no distinction between strings enclosed in quotes ("password") and those enclosed in apostrophes ('password') so that's nothing to do with the problem.

no that's my bad- i missed that oops... granted we can;t see the rest of the code so maybe that isnt the issue; but still i missed him declaring "capt"... but (and this may be ignorance on my part) how does the script know "what value" to pull from the element ID of caption? idk, maybe it's bc i am so used to C# flipping out about every single little thing, but in my mind i would have to tell the script what value to pull... something like this.getElementById("caption").text or something?
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Old 07-15-2011, 01:09 PM   PM User | #5
sam9461
New Coder

 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
sam9461 is an unknown quantity at this point
Great thanks to Old pedant, alykins for your helps.

I agree with Old pedant, “JavaScript makes no distinction between strings enclosed in quotes ("password") and those enclosed in apostrophes ('password')”, I run other javascript programme using apostrophes, no problem at all.

In regard of the element with the ID of “Caption”, I post part of the coding in my PHP webpage for your reference.
……….
<?php
$cap=$_GET['cap'];
?>
…………
<tr>
<td>&nbsp;</td>
<td bgcolor="#FFCC66"><span id="caption" class="STYLE36"><?php if($cap==1){echo"To get your ID, please provide the following information";}else{echo"To get your password, please provide the following information";} ?></span></td>
<td>&nbsp;</td>
</tr>

You can see, the element with the ID of “caption” is a span.

When the link “I’ve forgotten my ID” is clicked (i.e., cap =1), the following page come out:


To get your ID, please provide the following information

Your email address












When the link “I’ve forgotten my password” is clicked (i.e., cap =2), the following page come out:


To get your password, please provide the following information

Your email address












I want to transfer cap=1 or 2, to the PHP webpage first, then transfer it to the email to the user if the user fill his email address right and give right answer to the secret question. When the user click the link embedded in the email, a new PHP web page will come out, but its caption (showing ID, and login; or set up new password) will be controlled by cap=1 or 2.

However, when the link “I’ve forgotten my ID” or “I’ve forgotten my password” is clicked, and cap =1 or 2 has been transferred to the PHP webpage, no element can be used to transfer the value by Javascript and Ajax. My choice is to use:

var capp = capt.indexOf('password');
if (capp < 0) {
ccap = 1;
} else {
ccap = 2;
}


If you have better solution for me, perhaps, “var capp = capt.indexOf('password');” could be avoided.

Thanks again for your help.
sam9461 is offline   Reply With Quote
Old 07-15-2011, 01:41 PM   PM User | #6
sam9461
New Coder

 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
sam9461 is an unknown quantity at this point
Hi, just let you know, I followed alykins' suggestion, and changed:

var capt = document.getElementById("caption").value;

to:

var capt = document.getElementById("caption").text;

then test it, the problem is still there.

Any suggestions are extremely welcome, thanks!
sam9461 is offline   Reply With Quote
Old 07-15-2011, 01:48 PM   PM User | #7
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
Quote:
Originally Posted by sam9461 View Post
Hi, just let you know, I followed alykins' suggestion, and changed:

var capt = document.getElementById("caption").value;

to:

var capt = document.getElementById("caption").text;

then test it, the problem is still there.

Any suggestions are extremely welcome, thanks!
i don't think .text is a correct usage in javascipt... i mainly do C# and am now diving into js; i was moreover trying to drive the point of i think you need to extract the text from it... or maybe put it into an input field? i just am not seeing how he script is determining what "value" to pull.
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Old 07-15-2011, 11:23 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,570
Thanks: 62
Thanked 4,061 Times in 4,030 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Code:
var capt = document.getElementById("caption").innerHTML;
Only <form> fields have a .value property. Only <option>s have a .text property.

Specifically:
Code:
<form ...>
    <input name="bonzo" value="xyz" />
    <textarea name="yowser">This is it</textarea>
    <select name="foo">
        <option value="1111">One one one</option>
    </select>
</form>
RED shows value propery, BLUE shows text property.

The content of non-form fields such as <span>...</span>, <td>...</td>, <div>...</div>, <b>...</b> (where the content is represented there by ...) is always the innerHTML property.
Old Pedant is online now   Reply With Quote
Old 07-16-2011, 02:49 AM   PM User | #9
sam9461
New Coder

 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
sam9461 is an unknown quantity at this point
Hi, Old Pedant, you are great! I did as you guided, 'innerHTML' was used, then the problem was gone! Thank you so much for your great help. Also thanks to alykins' quick response.

Now, when I am running the code, another problem come out: 'exit' is undefined, (Line:42, char:3; code:0). Should I remove the 'exit' out? I really appreciate your further contribution.
sam9461 is offline   Reply With Quote
Old 07-16-2011, 03:36 AM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,570
Thanks: 62
Thanked 4,061 Times in 4,030 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
exit is not a keyword in JavaScript. It is a keyword in other languages.

*Probably* you can just use return in place of exit but I haven't looked that close at your code.
Old Pedant is online now   Reply With Quote
Old 07-16-2011, 03:37 AM   PM User | #11
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi


Quote:
another problem come out: 'exit' is undefined,
I think 'exit' is PHP ---- so I think you should change it to

Code:
return false;
LT

Last edited by low tech; 07-16-2011 at 04:47 AM..
low tech is offline   Reply With Quote
Old 07-16-2011, 04:04 AM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,570
Thanks: 62
Thanked 4,061 Times in 4,030 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yeah, return false if this code is to be used in the middle of validation. <shrug>Can't hurt to do that, in any case.

Last edited by Old Pedant; 07-16-2011 at 04:07 AM..
Old Pedant is online now   Reply With Quote
Old 07-18-2011, 03:25 PM   PM User | #13
sam9461
New Coder

 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
sam9461 is an unknown quantity at this point
[Resolved] Javascript error: undefined is null or not an object (help please!)

Old Pedant, Low tech, thank you so much for your help! I am very glad to tell you that the problem has been sorted out with your help. YOU're brilliant!
sam9461 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:41 AM.


Advertisement
Log in to turn off these ads.