0

I have a form with allows the user to edit data in the database using Ajax and ASP. Not sure why the ajax call is not updating the database as requested.

AJAX/jQuery code:

$('#editbtn').click(function(){
        var thisid = $('#edsid').val();
        var enm = $('#edtName').val();
        var eml = $('#edtEmail').val();
        var egp = $('#editSubForm input[name=edgrp]').val();
        var dataString = 'act=upd&sid=' + thisid + '&edName='+ enm + '&edEmail='+ eml + '&edgrp='+ egp;
        $.ajax({
        type: 'GET',
        url: '/subscr_query.asp',
        data:  dataString,
        success: function(){
                //alert(dataString);
                $('#successmsg').append("Edited!");
                $('#successmsg').fadeIn(1200, function(){$(this).fadeOut(2000);});
                },
        error: function(){
                $('#delpop').fadeIn(300);
                }
            });
    });

The form contains a submit button with the id "editbtn". Form seems to process with out the database getting update with the current information.

I've tested the queryString on the subscr_query.asp page by entering the data that gets outputted from the form to the page directly, and db updated as expected.

Any ideas?

2
  • 1
    Title says "MySQL" and tags say "SQL Server" - which one is it now?? Commented Nov 22, 2011 at 21:48
  • Sorry, it's actually SQL Server. But the database type doesn't matter in this case because the problem was the form processing before the Ajax had a chance to run. I changed the title of the question. Sorry again for the confusion. Commented Nov 24, 2011 at 14:57

1 Answer 1

2

I'm suspecting you're not preventing the default action; hence, it's submitting the form before performing the ajax request. Use preventDefault

$('#editbtn').click(function(e){
        var thisid = $('#edsid').val();
        var enm = $('#edtName').val();
        var eml = $('#edtEmail').val();
        var egp = $('#editSubForm input[name=edgrp]').val();
        var dataString = 'act=upd&sid=' + thisid + '&edName='+ enm + '&edEmail='+ eml + '&edgrp='+ egp;
        $.ajax({
        type: 'GET',
        url: '/subscr_query.asp',
        data:  dataString,
        success: function(){
                //alert(dataString);
                $('#successmsg').append("Edited!");
                $('#successmsg').fadeIn(1200, function(){$(this).fadeOut(2000);});
                },
        error: function(){
                $('#delpop').fadeIn(300);
                }
            });
      e.preventDefault(); // prevent default action
    });
Sign up to request clarification or add additional context in comments.

Comments

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.