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";
}
}
}
}