Remove Button and Field

Asked

Viewed 90 times

1

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

I have the code below via Javascript, when I do an Input recording the date in the database, I need the button and the input to disappear, permenecending only the date inputada:

    <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 above code works, but if I update the page, the buttons come back. There would be some way to remove the fields permanently?

  • The <?php echo $fetch['data_protocolo']; ?> is empty initially, before you make the Submit?

  • There is a structure in While that brings all the information from the database. Initially the Protocol Date field is empty, and then filled in. Once filled, I need the elements to be summarized (Input Text and Submit). And the ones that have not been filled, remain.

  • It is not enough to just disappear with the input and the button, you should also avoid new connection.

3 answers

0

When you make a new one request it will reload the elements again! Ie the DOM comes as if it were the first time you load this content. What is the expected behavior in this code. A suggestion: make a structure that changes some flag value, which you can check every time you reload the page! By CHECKING 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, a consumption of API´s, etc etc etc etc.

  • That’s what I figured. Grateful @Tiago

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>

0

Besides disappearing the button and the input, remaining only the inputada date, it would be important to avoid new connection to the bank using $_SESSION

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

  1. check if there is a Session, if there is no Session and the form is not only printed on the form screen
  2. if there is no Session, check that the form has been submitted
  3. if there is no Session and the form is submitted check if there is an email, if there is an existing email
  4. if it does not exist do the Insert 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"];
}
?>

Browser other questions tagged

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