0

We have a number of clients that have agreed to send us their form data once a form is submitted on their site. Is this possible and what is the best way to handle this? Our site is built in coldfusion while the client site varies.

I had the client add a script tag to include a javascript file from our server on their form page. Also had them add an onClick event to their form button so this javascript is called on submission of their form.

This is the javascript file:

function cpcshowElements(f) {
var formElements = "";
for (var n=0; n < f.elements.length; n++) {
    box = f.elements[n];
    formElements += box.name + ":" + f.elements[n].value + ",\n";
}
var track = new Image();
/*send data to us*/
track.src="http://XXX.net/form_record.cfm?form="+ formElements + "&self=" + this.location;  
}

On form submission the cpcshowElements function is called, formats the form data, appends it to the end of the XXX.net/...and calls that url. The form_record.cfm page basically does some checks and inserts the data into a table.

This process does work, however not consistently. The data doesn't always make it into the database. That is the problem. Is there another way to do this that won't have data loss?

  • How are you transferring the form results across domains? – mattacular Nov 30 '11 at 18:15
  • @mattacular Looks like a simple GET request when javascript sets the image source. Old technique for sending data still used today because it works. Big black hole with this issue is the server on the other end. No guarantees they are installing it where it needs to go. Also, if form is submitted by hitting `enter` then `onClick` won't fire. Should be `onSubmit` – jk. Nov 30 '11 at 18:55
  • All the code is stored on our server - the .js page (code above) and the xxx.net/form_record page. The only thing the client is doing is including the .js page on their form page plus adding the onClick call. I set up the call to the processing page (form_record.cfm) in the js kind of like a pixel image so that page should be called and the data should be inserted into the db. – user1073840 Nov 30 '11 at 19:23
  • `onClick` is not reliable for form submits. Have them use an `onSubmit` instead. Explained in answer below. – jk. Nov 30 '11 at 19:27
  • I don't know if it makes a different, but the onClick is on the form submit button not within the
    tag itself.
    – user1073840 Nov 30 '11 at 20:29
  • Then it may be that the form is submitting before you function is firing. It would be helpful to see the form code or a reasonable test version of it. – jk. Nov 30 '11 at 20:53

2 Answers2

2

The data getting to the database is pretty deep down the chain. The first step is to figure out where the request isn't coming through. Find the weak link, and then fix that part.

Chances are, there are other issues causing the failure than this piece of javascript. Test each part of the process and figure out where the problem lies. Chances are, it isn't in the javascript.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
2

Check whether the form on the serve is being submitted by method other than onClick. If the form can be submitted by hitting enter or tabbing and hitting enter or the spacebar, than you are missing some submits. Would work more consistently with onSubmit rather than onClick.

Example:

<form onsubmit="your_function_here">

Also, if the form is submitting and then moving on to another page, you javascript code may not have enough time to fire. In that case, put a delay into your function to allow the GET request for the image to be made before the page evaporates.

jk.
  • 14,365
  • 4
  • 43
  • 58