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

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 06-20-2011, 02:25 PM   PM User | #1
bonecone
New Coder

 
Join Date: Nov 2009
Posts: 53
Thanks: 3
Thanked 0 Times in 0 Posts
bonecone is an unknown quantity at this point
Question Ajax not submitting parameters

I've set up a cross browser ajax submission function that receives two parameters, a task name and a "this" reference to the html object that called the function.

submitbutton_ajax('taskName', this);

I've created two tasks. One of them is for a drop-down listbox that populates with links retrieved from the database. It then loops through the links and assigns a function to the onclick event of each one as below.

Code:
for(i=0; i < elem_links_ref.length; i++) {
   elem_links_ref[i].onclick = function(event) {
      submitbutton_ajax('selectOption', this);
   }
}
The drop-down listbox works, but I'm having trouble with the retrieved links. When I click on a link it manages to call the submitbutton_ajax function, but it fails to send a post request to the server. It sort of works when I insert the green segment of code at the bottom, but obviously not what I want.

Why is the xml http request failing to send unless I do this?


Code:
submitbutton_ajax('task_name', element_reference) {

// CODE GOES HERE FOR CREATING THE CROSS BROWSER XML HTTP REQUEST OBJECT CALLED "myRequest"
// AS WELL AS SETTING OTHER PARAMETERS BASED ON THE TASK BEING CALLED


// SET THE ONREADYSTATECHANGE EVENT

myRequest.onreadystatechange = function() {
   if(myRequest.readyState == 4) {
      if(myRequest.status == 200)
      {

         // POPULATE DROPDOWN LIST WITH LINKS //

         if(task == 'togglePopups') {
            elem_child_ref = elem_ref.getElementsByTagName('div');
            elem_child_ref[0].innerHTML = myRequest.responseText;
            elem_links_ref = elem_child_ref[0].getElementsByTagName('a');

            for(i=0; i < elem_links_ref.length; i++) {
               elem_links_ref[i].onclick = function(event) {
                  submitbutton_ajax('selectOption', this);
               }
            }

            elem_ref.className = 's_pullup';
            elem_child_ref[0].style.display = 'block';					
         }

         // CODE TO EXECUTE WHEN ONE OF THE LINKS ARE CLICKED
         // THIS CODE FAILS TO EXECUTE

         if(task == 'selectOption')
         {
            alert('toggled');
         }
      }
      else
      {
         alert('There was a problem with the request');
      }
   }

   // HOWEVER IT DOES WORK (SORT OF) WHEN I INSERT THIS CODE

   else {
      alert('AJAX IS MESSING UP AGAIN!');
   }

   // IT DISPLAYS THE "AJAX MESSING UP" MESSAGE SEVERAL TIMES
   // BUT EVENTUALLY ALSO DISPLAYS THE "TOGGLED" ALERT MESSAGE
}

   // SEND REQUEST

   myRequest.open('POST', url, true);
   myRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   myRequest.setRequestHeader("Content-length", params.length);
   myRequest.setRequestHeader("Connection", "close");
   myRequest.send(params);
}
bonecone 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 06:39 AM.


Advertisement
Log in to turn off these ads.