Search code examples
jqueryajaxform

Auto submit form using AjaxForm to show initial result set. A better way?


I'm using the JQuery plugin AjaxForm (http://jquery.malsup.com/form/) to submit the contents of a form to a PHP script and return the results to a div.

The form has some pre-set values in the form attributes, and when the page first loads I want to submit the the form so that the results div displays some results (and isn't just blank).

The code looks as follows:

$(document).ready(function() { 

    $('#searchform').ajaxForm({  
        target: '#searchresults', 

        success: function() { 
            $('#searchresults').fadeIn('slow'); 
        } 
    });

});

I've tried all the normal ways to submit a form using Javascript, such as the searchresults.form.submit(); approach, but it doesn't work.

Can anyone show me how I can amend the Javascript to submit the contents of the form #searchform on load? Or is their a better way to run the PHP script and return the results to the #searchresults when on load?


Solution

  • You need to trigger the submit event of the form in the document ready.

    $('#searchform').ajaxForm(function() { 
       // Handle stuff here 
    });
    
    $('#searchform').trigger('submit');