How to send data with JS? no page refresh

2

I'm trying to create an HTML Game and need a way to send data to a database (MYSQL) and would like to know how I can send site data to a server without the page refreshing.

If anyone knows some libraries I can use to develop a 2D game in html. Please express yourself.

Thanks in advance.

    
asked by anonymous 21.11.2017 / 04:18

1 answer

3

To send data without your page refreshing, refresh or something. You need 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><scripttype="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 this you will have to change your form tag, in case if it is so

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

You have to leave this, as I will show below, after all in JavaScript will already be indicating the name of the file and the sending method that will be POST

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

Remembering that in this case you should have a submit to work sending the data correctly, I'll give an example below a submit

 <input type="submit" value="Salvar informações"> 
    
21.11.2017 / 04:40