Remove Button and Field

1

Is there any way to remove fields with definite in Jquery?

I have the following code via Javascript, when I make an Input recording the date in the database, I need the button and the input to disappear, only allowing the input date:

    <td>
     <?php echo $fetch['data_protocolo']; ?>
    <br>
    <br>
  <input  name="protocolo" id="protocolo" type="text" placeholder="" style="width:100px;font-size: 13px" class="form-control input-md">
 <br>

  <input id="btn" type="submit"  class="btn btn-primary" value="OK" />
  </td>



<script>

var btn = document.getElementById('btn');
btn.onclick = function () {

document.getElementById('protocolo').remove();

 this.remove();
};

</script>

The code above works, but if I refresh the page, the buttons come back. Would there be any way to permanently remove the fields?

    
asked by anonymous 18.12.2017 / 21:36

3 answers

0

When you make a new request it will reload the elements again! In other words, DOM comes as if it were the first time you load this content. What is the expected behavior in this code. One suggestion: Make a structure that changes some flag value, which you can check every time you reload the page! When VERIFYING whether it is true or false, you can determine whether it should appear or not. You can approach several strategies for this: Values in session , consumption of API´s, etc etc etc.

    
18.12.2017 / 21:44
0

You can hide the HTML part (field and button) if the value in $fetch['data_protocolo']; is not empty:

<td>
   <?php
   echo $fetch['data_protocolo'];
   if(empty($fetch['data_protocolo'])){
   ?>
   <br>
   <br>
   <input  name="protocolo" id="protocolo" type="text" placeholder="" style="width:100px;font-size: 13px" class="form-control input-md">
   <br>
   <input id="btn" type="submit"  class="btn btn-primary" value="OK" />
   <?php
   }
   ?>
</td>
    
18.12.2017 / 23:00
0

In addition to removing the button and the input, leaving only the date entered, it would be important to avoid re-connecting to the bank using $_SESSION

I assumed that there is some unique data in the table (in the example below it would be the email)

  • check if there is a session, if there is no session and the form is not submitted just print the form on the form
  • If there is no session, check if the form has been submitted
  • If there is no session and the form is submitted check if there is an email,
  • If it does not exist insert it and create a session
  • Code

    <?php
    session_start();
    if($_SESSION["insert"]==""){
    
       if(isset($_POST['email'])){
          $mysqli = new mysqli("localhost", "USUARIO", "SENHA", "NOME_DB");
          $email=$_POST['email'];
          $data_protocolo=$_POST['protocolo'];
          $sql2=("SELECT * FROM tabela where email='$email'");
          $result = mysqli_query($mysqli,$sql2);
    
            while($fetch= mysqli_fetch_assoc($result)) {
              $data_protocolo=$fetch['data_protocolo'];
              echo $data_protocolo;
              $resultEmail=$fetch['email'];
            }
            if ($resultEmail==""){
               $mysqli->query("Insert into tabela (email,data_protocolo) values ('$email','$data_protocolo')");
               $_SESSION["insert"] = $data_protocolo;
            }else{
               echo "<br>email ".$resultEmail." já existente";
            } 
       }
    
    }
    
    if($_SESSION["insert"]==""){
    ?> 
       <form role="form" id="meuForm" action="" method="post">
       <input  name="email" id="email" type="email" placeholder="email" style="width:100px;font-size: 13px" class="form-control input-md" value="" required> 
       <input  name="protocolo" id="protocolo" type="date" placeholder="" style="width:100px;font-size: 13px" class="form-control input-md" value="" required>
       <br>
       <input id="btn" type="submit" class="btn btn-primary" value="OK" />
       </form>
    <?php 
    }else{
      echo $_SESSION["insert"];
    }
    ?>
    
        
    19.12.2017 / 00:00