send form to another html page

Asked

Viewed 69 times

-4

I have a form and I want to send it to another page, which is a contract. The data sent is to fill out the contract. I used the attached script, however, it only sends a field.

function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, '\\$&');
        var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
        results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, ' '));
        }

        var sn = getParameterByName('pt1');
        document.getElementById("vj2p1").innerHTML = sn;

  • The following code is just a function to pick a parameter, could send the complete code of the script you are trying to implement?

  • The other part is an HTML, where there is a <form action="destination.html" method="GET>". The form that this script belongs to has only one field. I am trying to adapt it in another that has 16. I already increased the number of "var Sn" but I was not successful.

  • It would be easier to use php or json, but since you chose javascrip, it would not be better to use ajax/jquery to do this upload?

1 answer

1

Explanation:

When you send the form from page 1 to page 2 it creates "Parameters" at page 2 url

For example: http://127.0.0.1/pagina2.html?nome=Fulano&nascimento=01/01/1990

The function you are using takes these parameters from the url, but only one at a time.

   function getParameterByName(name, url) {
       if (!url) url = window.location.href;
       name = name.replace(/[\[\]]/g, '\\$&');
       var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
       results = regex.exec(url);
       if (!results) return null;
       if (!results[2]) return '';
       return decodeURIComponent(results[2].replace(/\+/g, ' '));
   }

That is, if you want to take more than one parameter and put it somewhere, you will need to put one by one on page 2

As in the following example:
If on your page 2 there is an html like this:

    <div class="conteudo">
        Seu nome é: <span id="nome"></span><br>
        Sua data de nascimento é: <span id="nascimento"></span>
    </div>

To print the parameter values on each span of these, you will need to identify each span, as I did by giving an "id" pro span.
for example, if you want to take Cpf, it would be as follows: <span id="cpf"></span>

Understood this part, I could see that you will have to create a variable to save the parameter and then insert it in the desired field
See the following code to insert the "name" and "birth" fields in the html I used in the previous example:

    // Campos que você quer pegar e exibir na página
    let nome = getParameterByName('nome');
    document.querySelector("#nome").innerHTML += nome;
    let nascimento = getParameterByName('nascimento');
    document.querySelector("#nascimento").innerHTML += nascimento;

In this case, your url would need to be something like http://127.0.0.1/pagina2.html?nome=Fulano&nascimento=01/01/1990 to display the name and birth in the fields

If you want to add more fields, you will have to add more variables that save the parameters and more insertion blocks in html, like this example to insert Cpf in <span id="cpf"></span>

To insert the Cpf:

    let cpf = getParameterByName('cpf');
    document.querySelector("#cpf").innerHTML += cpf;

Remembering that to display Cpf, your url would need to be something like: http://127.0.0.1/pagina2.html?cpf=ExemploDeCPF

  • I must put pag 2 id’s equal to pag 1?

  • It’s not mandatory, but if you want it, it’s easier for you to understand :)

  • Didn’t work out!!

  • Could you post the code from page 2? I would like to see how you implemented :)

  • Jassriver, now it worked out, I fixed my mistake. Page 2 code is what’s posted. My error: The name of var Sn is the name of the input on page 1, I was putting the name on page 2. But something happens that I don’t want to happen on this page that I’m using: When the form is submitted, it automatically goes to page 2. I don’t want it to do that, because page 2 is a PDF and I will try to make sure that when the form is submitted, automatically, it sends the PDF to the email I will request in the form. I already have such a system, but using google Sheets.

  • @jairoolindino for sending email, you would need a server-side language like PHP, with it, you could send this PDF to the form email :)

  • Com js tbm dá. I use this code in google Sheets: the getBlob var pdf1 = Driveapp.getFileById(Wb.getId()). getas("application/pdf"). getBytes(); var Anexo1 = {filename:"",content:pdf1, mimetype:'application/pdf'}; // here is the email submission routine Mailapp.sendemail({ to: date[data.length - 1], cc: "", Subject: "", htmlBody: "<H2>SIMULADO</H2>", name: ', attachments: [Anexo1] }); //calls the return page . Success Return Htmlservice.createHtmlOutputFromFile('). getContent(); }

  • @jairoolindino que daora! I didn’t know this method! I’ll find out about it, but otherwise, your doubt has been resolved, or there’s something else I can help you with?

  • The first yes. Now I just want that when the form is submitted the user is not taken to the PDF page, it should be redirected to a thank you page.

Show 4 more comments

Browser other questions tagged

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