I have an application which allows the user to switch between 8 different form pages without losing their data. When the user has filled in some information they click a link which calls an ajax function to update the information in the database.

It is all working fine apart from one little glitch.

When a user goes to page 1 to add a new record there is no session variable for the current record. As soon as they move to another page the script that is called through ajax inserts the information into the database gets the auto incremented id and sets the session variable to this, which is then referenced throughout the rest of the pages.

If the user moves onto another page, e.g. page 2 it checks to see if a session variable is set for the current record being worked on and if not it re-directs the user to the home page.

I think what is happening is that the user arrives at page 2 before the ajax script for page 1 has finished processing and as such the session variable is not set in time. All the information has been put into the database but the re-direct still happens back to the home page.

I have tried adding sleep to the beginning of page 2 but that hasn't helped to give it enough time to have the session variable set but that didn’t work.

I have also tried running a quick query just to get the auto incremented id but that also hasn’t solved it.

Does anyone else have any suggestions on how to overcome this problem?

Thanks for any help.

    I'm fairly new to working with AJAX and a little unclear on your problem. It sounds like you are redirecting before the receiving a response. So, basically, instead of redirecting with PHP you redirect with JavaScript. Setup your AJAX function to redirect ONLY if readyState==4.

    Without any code, it's hard to help any further..

      vert,

      I don't know the nature of your forms, but I have a suggestion. If the problem you are having is occurring for the reason you think it is occurring, you could always require the first form to be very basic information and it could be submitted normally (without AJAX). That way you would ensure forms 2-8 would have a session. The first form could start with basic information (whatever that is for you...), such as first name, last name and email.

      That work?

      @

        Thanks Matt and Chad,

        Chad's suggestion is exactly what I've done. The user fills in one field which is submitted as a normal form and then there are no problems with the session variable being set.

        I'd still like to see if there is a way to do it without this page and will post if I acheive it.

        Thanks for your time.

          Write a Reply...