Sequential ajax background process after submit

The script message is working fine, it just executes my php code in the background when the page loads. What I can not do is run php in the background after clicking the submit button. I tried adding a form to the body, but I don’t know how to associate it with ajax .. how can this be done? thank!

<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>
+3
source share
1 answer

You execute the code when the document is ready to download. You must add this code to the button click event.

So instead :

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

Do this :

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

All Articles