1

Not sure why the following doesn't work. I hope somebody can shed some light.

I have a global parameter reresenting a certain id of an item in a database. Now in order to check if the db should be updated or a new entry should be made for the item, I check before making the ajax call if the variable is set. If the variable is set, I know I have to update the db, if its not set I know I have to insert into the db. Then on the server side I return a newly generated id in case of an insert or else just the old id in case of an update. I use the jQuery form and validate plugins by the way (hence the function call ajaxSubmit()). However, the assignment in the success function of submitHandler doesn't seem to work (I have checked that data.poiId returns a suspected result).

$(document).ready(function() {

    // Function which examines window.location.href and extracts a numerical id if there is one specified in the query
    var id = getParameter('poiId')

   ...

   },
   submitHandler : function(form) {
       var str = 'submitPOI.php';
       if (id.length > 0) str += '?poiId=' + id;

       $(form).ajaxSubmit({
           url : str,
           type : 'POST',
           dataType : 'json',
           success : function(data) {
               id = data.poiId     // Update POI id with response from server
           } 
       });
   }

   ...
});
3
  • did you check with debugger on line "id=data.poiId" is id visible (is it undefined or not)? Commented Jun 13, 2011 at 13:53
  • You might be continuing before the AJAX returns. Once the AJAX call is made the code does not wait for it to return before continuing. What are you trying to do with the id? Commented Jun 13, 2011 at 13:54
  • It is to prevent that another entree is created when the user submits the form again right after he has submitted it the first time (on the first submit id has no value yet). I suppose it could be a timing issue, although the second submit is well after the success function has been called. Commented Jun 13, 2011 at 13:59

1 Answer 1

4

Seems to me that your id variable is not global as it is declared in the $(document).ready() function. Move it outside the function and check if it works.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow! How stupid of me... Thanks for pointing that out. Works like expected now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.