Forum Moderators: open

Message Too Old, No Replies

Javascript - PHP - MYSQL interaction

Getting form data into a MYSQL database after javascript onchange call

         

lgn1

4:47 am on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We are in the situation where we don't want to wait for a form submit to occur for data from html form fields to be loaded into our MYSQL database. As some people are abandoning the form before the submit button, and we want to collect as much info as possible for form improvement and sale rescue procedures.

We are looking at tying a javascript onchange script on each one of our text fields, and have the onchange submit the data to the MYSQL database, after each text field is completed.

Since javascript is client side and MYSQL and PHP is server side, its not straight forward.

Im sure this has been done before, and I can find bits and pieces on the web, but not an easy to understand complete solution.

Any body have some sample code, or some good references to handle javascript-php-mysql interaction

DrDoc

5:13 am on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, interesting problem. The way I see it you have two options. The obvious one would be some form of AJAX solution. You can have a hidden iframe somewhere on your page, and change the src document upon
onchange
of a form field. Another way would be to instantiate an XML object call (or similar).

What I would probably do, if I were you, is to generate a unique ID (as a hidden value) for each form. Then submit the ID along with the name and value of the field that was changed. This way you don't need to submit the entire form each time, but can still keep track of which form data is all part of the same session.

1px iframe is the way I would do it. Easy to set up. Easy to manage.

lgn1

4:15 am on Feb 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was thinking of it some more, and there are two cases when I want the javascript to execute.

1) if the user closes the browser session
2) if the user hits the back button

Sending the data to the database on every form entry would be overkill.

In the past I have seen this used for malicious purposes, and I don't want to trigger any popup warning messages. As all we are trying to do is save the user data, not create a popup storm, etc. ie. I want to keep it legimimate and transparent to the user.

I very rarely use javascript, as Im a server-side scripting person. Can the javascript action on back button detect or browser exit be done in modern browsers?

DrDoc

4:23 am on Feb 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No need for popups. Hidden iframe should create no warnings at all.

Use a body

onunload
... But, of course, use a check variable which is set to something; upon normal submittal the variable should be set to something else. Let the onunload function check if the variable is still set to its default value of something, and only then post the code to the iframe.