how to post data with on load function with Jquery

Dear Experts,

I'd like to post the data to my php page. The below code works but I want to post it with on load function not with on click.
I want to post it when the page is loading.
How can I do that? thank you in advance.

I also want to remove the alert.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.post("test-post.php",
        {
          name: "Donald Duck",
          city: "Duckburg0922"
        },
        function(data,status){
            alert("Data: " + data + "\nStatus: " + status);
        });
    });
});
</script>

Open in new window

LVL 1
BRMarketingAsked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
tried this?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
       $.post("test-post.php",
        {
          name: "Donald Duck",
          city: "Duckburg0922"
        },
        function(data,status){
            alert("Data: " + data + "\nStatus: " + status);
        });
});
</script>

Open in new window

1
 
BRMarketingAuthor Commented:
you are great. thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.