Anchor validation for download

Asked

Viewed 84 times

2

foto do formulário para download

Next, I need to validate this form so that only after the person registers can they download the file. With the tag I can already have this validation but I was able to put to download only with the tag "anchor".

<section id="e-book">
    <header>
        <h3>Baixe nosso e-book</h3>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod<p>
    </header>
    <form id="for-banner" class="form-inline">
      <div class="form-group">
        <label for="exampleInputName2"></label>
        <input type="text" class="form-control" id="exampleInputName2" placeholder="Nome Completo" required>
      </div>
      <div class="form-group">
            <label for="exampleInputEmail2"></label>
            <input type="email" class="form-control" id="exampleInputEmail2"    placeholder="E-mail Corporativo" required>
        </div>
    <div>
        <a href="/e-book/teste-ebook.pdf" download id="btn-ebook" type="submit" class="btn btn-default">Download</a>
    </div>
    </form>
</section>
  • And what was the script in Javascript you thought about using?

  • actually thought better here, the button does it here already, and checks if the email is already registered in the database, need to know if the <button> tag of the html has to download also equal this <a tag>

2 answers

1

If she’s going to register, it means you’re going to have a back-up involved, with that in mind, just send a flag return and check, Ex:

document.getElementById('btn-ebook').onclick = function(){
   // Aqui teria seu ajax, ex utilizando axios
   axios.post('SUA URL', {
      // PARAMS
   })
   .then(response => {
      // vamos supor que sua api tenha retornado o ok, e a URL
      if(response.status == 1){
        window.open(response.url, "_SELF")
      }else{
        alert('Você precisa se cadastrar antes')
      }
   })
   .catch(error => {
      console.log(error)
   })
}
<section id="e-book">
    <header>
        <h3>Baixe nosso e-book</h3>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod<p>
    </header>
    <form id="for-banner" class="form-inline">
      <div class="form-group">
        <label for="exampleInputName2"></label>
        <input type="text" class="form-control" id="exampleInputName2" placeholder="Nome Completo" required>
      </div>
      <div class="form-group">
            <label for="exampleInputEmail2"></label>
            <input type="email" class="form-control" id="exampleInputEmail2"    placeholder="E-mail Corporativo" required>
        </div>
    <div>
        <a href="#" download id="btn-ebook" class="btn btn-default">Download</a>
    </div>
    </form>
</section>

And that way, you’d get your validation.

0

Guys I managed to solve otherwise instead of downloading the e-book I put to access it online with the following code:

<section id="e-book"> <header> <h3>Acesse nosso e-book</h3> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod<p> </header> <form method="post" action="images/Instalação do Microsoft SQL Server 2014 Express.pdf" download id="for-banner" class="form-inline" target="_blank"> <div class="form-group"> <label for="exampleInputName2"></label> <input type="hidden" name="ipaddress" id="ipaddress" value="<?= $user_ip; ?>"> <input type="text" class="form-control" id="exampleInputName2" name="nome2" placeholder="Nome Completo" required> </div> <div class="form-group"> <label for="exampleInputEmail2"></label> <input type="email" class="form-control" id="exampleInputEmail2" name="email2" placeholder="E-mail Corporativo" required> </div> <div> <button id="btn-ebook" type="submit" class="btn btn-default"> acessar agora </button> </div> </form> </section>

This way the user will only be able to access the e-book if he makes the registration. Thanks to all.

Browser other questions tagged

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