Is this possible? If so, I could really use some help on this. I’m very new to JavaScript and thus hardly know any of the syntactical specifications, nor proper methodology.
Some functions I wrote in an external file.
var base = base || {};
base.requestAjax = function () {
try{
return new XMLHttpRequest();
} catch (e) {
try {
return new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Something is wrong with your browser! Please ensure that you have javascript functionality enabled");
return false;
}
}
}
}
base.onReadyStateChange = function(ajaxRequest, formName, formArray, method, controller) {
var methodVerified = verifyMethod(method);
if (!methodVerified) {
throw new Exception("Error: please make sure the method passed matches that of \'GET\' or \'POST\'.");
}
for (input in formArray) {
document.formName.input.value = ajaxRequest.responseText;
}
ajaxRequest.open(method, controller, true);
file.send(null);
}
base.writeDropDownList = function(file, method, path) {
var file = upload.requestAjax();
file.open(method, path, true);
if (file.readyState != 4) {
throw new Exception("Error: text file ready state not equal to 4!");
}
if (file.status != 200) {
throw new Exception("Error: text file status not equal to 200!");
}
var lines = file.responseText.split("\n");
document.writeln("<select>");
for(line in lines) {
document.writeln("<option>" + line + "</option>");
}
document.writeln("</select>");
file.send(null);
}
base.verifyMethod = function(method) {
var methodLower = method.toString().toLowerCase();
switch(methodLower) {
case "get":
case "post":
return true;
default:
return false;
}
}
The html code which attempts to implement it
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../includes/css/adsmanager.css" />
<script type="text/javascript" src="../includes/js/base.js">
<!--
function createCountriesList() {
var file = base.requestAjax;
var path = "../includes/_notes/countries.txt";
base.writeDropDownList(file, "GET", path);
}
//-->
</script>
</head>
<body>
<form name='adsManager'>
<button type="text" value="test" onclick="createCountriesList()" />
</form>
</body>
</html>
I plan to use the functions for other things, so I figured I’d create a namespace. I found this as a reference and based most of my model off of it.
Thanks to all who can help.
What is your point of failure? Is your Javascript sending the Ajax request and receiving the response?
Is
linesgetting data in this line of your code?If you are getting this far, iterate through
linesand add options like this:In your
writeDropDownListmethod, I made a few changes:added a method that will be called when your Ajax call has completed. In that method, you should check the response string and add the options to your select box
In your code, you are checking and using
files.responseTextbefore you have even sent the Ajax request atfiles.send(null)EDIT:
Some more comments regarding your code:
In the
createCountriesListfunction, you createfileand assignit a value by calling
requestAjax. You then pass it towriteDropDownListfunction, where you assign it a value again bycalling
requestAjax. You see that this is redundant? There is noneed to create
fileincreateCountriesListand pass it as anargument. Create it just once in
writeDropDownList.in
writeDropDownListyou callupload.requestAjax(). What isupload. I don’t see you initializinguploadanywhere. Do you meanto call
base.requestAjax()?you have a function
base.OnReadyStateChangebut at no point are youtelling your AJAX request to call that function when state changes. See the code I posted above. The function I added called
requestCompletedoes that, and I tell the AJAX request to call it usingfile.onreadystatechange = requestComplete;You set
methodtoGET, yet you are not passing any GET values in your URLin
file.open(method, path, true);, path is supposed to be the URL of the script the AJAX request will call. You have setpathto../includes/_notes/countries.txt. An AJAX request cannot call a.txtfile since they do not execute.I just had a look at the source of your code, and it is all sorts of broken. Please do not use it.
What is
countries.txt? Are you attempting to populate a dropdown with a list of all countries, or some countries depending on user’s input?If the former, there is no need for Javascript / AJAX. You need to add PHP code in your html to populate the select box.
If the latter, your AJAX request should be sending the user input as a GET variable.