Advanced Search

Results 1 to 4 of 4

Thread: JQuery Ajax - Posting to Database

  1. #1
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    275
    Thanks
    3
    Thanked 17 Times in 17 Posts

    Default JQuery Ajax - Posting to Database

    The problem isn't with the php nor posting to the database. It's what I have to do to post to the database.

    It works with this code:
    Code:
    $("#sub").click( function() {
    		$.ajax({
    			type: "POST",
    			url: "userInfo.php",
    			data: $("#myForm").serializeArray(),
    			success: function(data){
    				alert("Win");
    			}
    		});
    		alert("Your data has been saved!");
    	});
    DOESNT work with this code:
    Code:
    $("#sub").click( function() {
    		$.ajax({
    			type: "POST",
    			url: "userInfo.php",
    			data: $("#myForm").serializeArray(),
    			success: function(data){
    				alert("Win");
    			}
    		});
    		//alert("Your data has been saved!");
    	});
    Any reasons why that's happening?
    Last edited by Deadweight; 12-08-2013 at 10:50 PM.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,704
    Thanks
    43
    Thanked 3,133 Times in 3,097 Posts
    Blog Entries
    12

    Default

    Technically speaking, unless I missed something, there's no difference between those two except the 2nd alert is commented out in the second example. However, there's much I cannot see. I can see that this code does not prevent the default action of the form. So perhaps the form is submitting in one or the other of the cases or in both cases but in different sequence thus changing the overall result.

    Generally when we wish to pass POST data to a PHP script, we .serialize() the form data, not .serializeArray() it. That might have something to do with it. Try:

    Code:
    $("#sub").click( function(e) {
    		e.preventDefault();
    		$.ajax({
    			type: "POST",
    			url: "userInfo.php",
    			data: $("#myForm").serialize(),
    			success: function(data){
    				alert("Win");
    			}
    		});
    		//alert("Your data has been saved!");
    	});
    The browser cache may need to be cleared and/or the page refreshed to see changes.

    If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    275
    Thanks
    3
    Thanked 17 Times in 17 Posts

    Default

    That actually fixed the problem. It didn't matter which serialize i used.
    Can you explain what preventDefault does?
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,704
    Thanks
    43
    Thanked 3,133 Times in 3,097 Posts
    Blog Entries
    12

    Default

    In this case it stops the default action of the event (e) that occurs whenever there is a click on the element with the id of sub. Like, if that would have submitted the form in the normal fashion, that's prevented. Or if it would have navigated to another page, that's prevented, etc.

    I can't be any more specific without seeing the page.

    See also:

    http://api.jquery.com/event.preventDefault/
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 4
    Last Post: 08-26-2012, 02:21 PM
  2. Ajax Tabs Content Script (v 2.2) - Posting form problem ...
    By stfrantic in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 02-22-2010, 07:01 AM
  3. Posting data to a Ms Access database using Ajax
    By twardone in forum The lounge
    Replies: 0
    Last Post: 03-17-2008, 07:41 PM
  4. Problem posting to database
    By goflish in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 12-05-2007, 08:59 PM
  5. about the session content and ajax posting..
    By Dennis_Gull in forum PHP
    Replies: 0
    Last Post: 08-28-2007, 11:23 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •