Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catch Form Submission before it's too late 1

Status
Not open for further replies.

tlhawkins

Programmer
Dec 28, 2000
797
US
Hi,

I'm catching all form submissions for a last minute AJAX call:
Code:
for(var c=0;c<document.forms.length;c++)
{
  if(window.addEventListener)
  { 
    document.forms[c].addEventListener('submit', onSubmitH, false); 
  } 
  else 
  { 
    document.forms[c].attachEvent("onsubmit", onSubmitH); 
  }
}
The catch works fine in IE and FF (my main targets). Here's the thing though, if I add an Alert to the onSubmitH function then everything within that function is executed, if I don't then nothing happens. It's supposed to call the server and write an entry to the DB. The DB entry gets written as long as the Alert is in there, but nothing if it's not.

It seems like the page is getting submitted too quickly and that the user interaction (the Alert) gives time to finish the process, is there anything else I could add in there that would keep page control long enough to finish the ajax call but not involve user input?

Thanks

Travis Hawkins
 
You could try making your AJAX calls synchronous instead of asynchronous...

How you do this depends on how you code your AJAX (yourself, using a framework, etc).

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Thanks for taking a look...

I'm actually calling a server on a different domain so I'm attaching the data to an IMG tag. I also noticed that it usually works in IE either way, but FF doesn't work at all unless I add an alert after the outbound call.

The call to the server is only sending data, not receiving anything so there isn't really anything to wait for. I just need to be sure that it gets the call out before the page stops executing.

Any idea what could hang on to page control without requiring user input?

Thanks

Travis Hawkins
 
ouch Dan,

I guess I have a looser interpretation of AJAX than you do. I remember when we were doing this by adding a Script block to the DOM and calling a server side script to make the JS to affect the page, then one day someone came up with the XMLHTTP and suddenly we were calling it AJAX. Not sure where it turned to AJAX, but here is my code for sending data from the clientside to the serverside:

Code:
function sendRequest(action, data) {
  var poststr = "action=" + encodeURL( action ) + "&" + data + "&sess=" + s_id + "&dom=" + domainID;
  var el = document.getElementById('wfaTrackImg');
  el.src = "[URL unfurl="true"]http://www.beachbumsoftware.com.com/webform/server_side.php?"[/URL] + poststr;
}
}

Thanks for taking a look. Again, if I add an Alert at the end of this process it takes place, if I don't then it doesn't in FF.



Travis Hawkins
 
Why not send that data as part of the form submission, given that it's being sent at the time of submission anyway? You could create some hidden fields to store that information.

Doing that would also require only 1 request instead of 2...



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
This is when I start a long winded explanation and after 10 posts someone says, "Yeah, you can't do that".

The data I'm gathering isn't being sent to the server that it's being submitted to. There are several sites that I'm monitoring the forms on. I don't have access to the server that the data is being submitted to, that's the client's server. The client has me monitoring the forms, I'm sending usage data to my server where I'm building reports. So, I can't use hidden form variables, and I can't put a repeater on the server that has the form. I need this to work like I have it.

Any chance?

Travis Hawkins
 
You could still use an [XMLHTTP] AJAX request and make it synchronous... that way, the execution would stop until the GET/POST had finished.

Dan





Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
XMLHTTP doesn't like it because the target is a different domain. Security limitations strike again. Although... if I just hit a script on their server that should be a 404 maybe that WOULD work to stop the page for a second, I'll try that and let you know what happens.

Thanks a ton for sticking with me on this

Travis Hawkins
 
Sweet, It worked. I have a dummy XMLHTTP request going off to the source server (so no security issue), it just hits a page that shouldn't be there but it manages to allow the PsuedoAjax call to get through correctly.

Thanks a Ton Dan

Travis Hawkins
 
XMLHTTP doesn't like it because the target is a different domain.
There are several ways around the same-domain policy that I am aware of:
- use on-demand javascript
- use a server proxy

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
right, I can't use XMLHTTP to send the data to the destination server because it's a different domain. But I can use XMLHTTP to send a completely useless request to the correct domain in order to delay the page for a sec, so my psuedoAjax call can get out before the page is gone.

It's probably a 9.8 on the Hacky Scale, but it works.

Travis Hawkins
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top