PDA

View Full Version : Basic Ajax Routine: noob help please



klaypigeon
06-28-2009, 08:33 PM
1) Script Title: Basic Ajax Routine (get & post)

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex17/ajaxroutine.htm

3) Describe problem: This is my first go around with ajax and I am really trying to make sense of the basic script. I can follow along ok thanks to the notation. In the long run I need to build some cascading drop down 'selects', populated via mysql, but that is there and I am still here :)

anyway I am just trying to figure out how to implement the stock example on my web server. I think if I can figure out how to get the post values I can work out the rest. But...I cannot get the script working. I cannot figure out how to call it. What is the code behind "Run Example" at the bottom of the page? I have ajaxroutine.js and postit.php in the same directory. This is what I have so far and it is not really doing anything.

<html>
<head>
<script src="text/javascript" src="ajaxroutine.js"></script>
</head>
<body>
<script type="text/javascript">
//Step 2: Define a "callback" function to process the data returned by the Ajax request:
function processGetPost(){
var myajax=ajaxpack.ajaxobj
var myfiletype=ajaxpack.filetype
if (myajax.readyState == 4){ //if request of file completed
if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
if (myfiletype=="txt")
alert(myajax.responseText)
else
alert(myajax.responseXML)
}
}
}

function createpoststring(){
var namevalue=document.getElementById("relatives").innerHTML //get value to post from a DIV
var agevalue=document.getElementById("myform").agefield.value //get value to post from a form field
var poststr = "name=" + encodeURI(namevalue) + "&age=" + encodeURI(agevalue)
return poststr
}

//Step 3: Invoke the Ajax routine method to make the desired Ajax request.
var poststr=createpoststring() //Get contents to post and create query string first
ajaxpack.postAjaxRequest("postit.php", poststr, processGetPost, "txt")
</script>

<h4 id="relatives">My uncle</h4>
<form id="myform">
Enter age: <input type="text" name="agefield" value="56" />
</form>
<a href="ajaxroutine.htm">Test</a> //this does nothing :)
</body></html>