In order to access the variable outside of the scope of the if statement
(anything between if(...) { here } ), you would have to declare it beforehand.
But since you are going to be assigning its value only in the event of ajax success, than I suggest passing it to a function that knows how to handle it.
Something like this:
var reg_email = document.getElementById("reg_email").value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var response_email_check = xmlhttp.responseText;
onCheckEmail(response_email_check);
}
};
xmlhttp.open("GET", "./lib/checkemail.php?str=" + reg_email, true);
xmlhttp.send();
And you have to define the function onCheckEmail
function onCheckEmail(response_email_check){
alert(response_email_check);
}
If the alert is not shown, then check to see if the ajax has been successful. You can do this by adding an else
to your if statement:
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var response_email_check = xmlhttp.responseText;
onCheckEmail(response_email_check);
}else {
alert("A server error occurred!");
}