Write to file . txt using Javascript

Asked

Viewed 39 times

0

I have a form HTML very simple (I am using it only for testing)

<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Form</title>
    </head>
    <body>
        <input type="email" placeholder="Digite o seu email" class="email">
        <input type="password" placeholder="Digite a sua senha" class="senha">
        <input type="button" value="enviar" class="confirma" onclick="MinhaFuncao()">
    </body>
</html>

Note that on the last line (line 12) my button has a function JS that up to now it is like this:

function MinhaFuncao(){
      var email = document.getElementById('email').value
      var senha = document.getElementById('senha').value
}

What I’d like to do is somehow take these values and write them into a file. txt
Unfortunately I have to really use a txt.
If such an act is not possible with Javascript, there is some way to pass the form information to a file python or C++?

  • 2

    for reasons of holding the browser can not write files, can create a code "server side" in python for example, receiving an http request, can if ajax for example, and so yes python can write a file, but note that it may not be on the same user’s machine

  • The most I can do is create and display a link (ancora) in the body of your HTML document whose user can right-click and choose to save this link as and what is saved is a dynamically generated text document (or anything else). I don’t know if it suits you.

1 answer

0

In Javascript it is unfortunately not possible to do this kind of thing (at least not in the client), instead try sending the information to a server using Ajax. Follow an example:

function getInformacaoQualquer() {
createRequest();
var url = "getInformacaoQualquerDoServidor-ajax.php";
request.open("GET", url, true);
request.onreadystatechange = atualizaPagina;
request.send(null);
}

This example I took from the code line, from a look that might help you: http://www.linhadecodigo.com.br/artigo/3585/ajax-basico-introducao.aspx

Browser other questions tagged

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