Tiutto
Tiutto

Reputation: 199

ajax background process after submit button

the script I post works fine, it just execute in background my php code at the load of the page.. What I cannot do is to start the php in background after having pressed a submit button.. I tried to add a form in the body but I don't know how to link it with the ajax.. how could be done? thanks!

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>

<script type="text/javascript">
  $(document).ready(function() {
    $.ajax({
        url: 'trial.php',
        beforeSend: function() {
            // you could also put a spinner img or whatever here too..
            $("#myStatusDiv").html("started..");
        },
        success: function(result) {
            $("#myStatusDiv").html(result);
        }
    });
  });
</script>

</head>
<body>

</body>                                                                                                             

</body>                                     
</html>

Upvotes: 3

Views: 2686

Answers (1)

Ropstah
Ropstah

Reputation: 17794

You are currently executing the code when the document is ready loading. You should add this code to the button click event.

So instead of this:

$(document).ready(function() {
    //your code
});

Do this:

$(document).ready(function() {
    $('#buttonid').click(function() {
        //your code
    });
});

Upvotes: 3

Related Questions