How to send data with JS? without updating page

Asked

Viewed 1,853 times

2

I am trying to create an HTML game and I need a way to send data to a database (MYSQL) and I would like to know how to send data from the site to a server without the page updating.

In case anyone knows some libraries I can use to develop a 2D html game. Please manifest.

From now on, thank you.

  • You can do this using javascript, but you have some code implemented ?

  • i have a code, but I don’t know how to do this function with pure js. if you know some way, pf help me

  • Edit the question and put your code if possible or part of it, but the question is, you’re using Submit in your form?

  • i still don’t have a code of that function ... before that I was passing the JS variables to PHP and sending to the bank.

  • Do what I will tell you I tested and it worked here and me a feedback, if positive mark the answer as best so that other users understand that this answer will resolve their doubt. It can help other users in the future

1 answer

3


To send data without your page updating, refresh or something like that. It is necessary to use javascript with a serialize .

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#ajax').submit(function(){
        var dados = $( this ).serialize();

        $.ajax({
            type: "POST",
            url: "nomedoarquivo.php",
            data: dados,
            success: function( data )
            {
                alert( data );
            }
        });

        return false;
    });
});
</script>

But for that you will have to change your tag form, in case it is like this

<form action="nomedoarquivo.php" method="POST">

You should leave so, as I will show below, after all in Javascript will already be indicating the file name and the sending method that will be POST

<form action="" method="" id="ajax">

Remembering that in this case should have a Submit to work the sending of data correctly, I will give an example below a Submit

 <input type="submit" value="Salvar informações"> 
  • It worked out man... thank you, you just saved a game in development.

  • I saw that you made a suggestion to exchange sucess for error, don’t do it. If it worked, don’t try to reinvent by changing the code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.