Enable "Submit" button only when data change

Asked

Viewed 36 times

-1

I would like to know how to enable the "Submit" button only when a different data is entered than the one returned from the bank. I think it’s simple, but I’m a beginner in the field and I’m not familiar with JS. The intention is that the "Submit" button is disabled if there is no change in the data.

Follow the form code:

<form action="update_bd.php" method="POST">
                        <div class="field">
                            <div class="control">
                                <input type="hidden" name="id_usuario" id="id_usuario" class="input is-large" required="" placeholder="ID" maxlength="3" value="<?php echo $linha['id_usuario']; ?>">
                            </div>
                        </div>
                        <div class="field">
                            <div class="control">
                                <input name="nome" id="nome" class="input is-large" required="required" placeholder="Nome" autofocus="" maxlength="15" value="<?php echo $linha['nome_usuario']; ?>">
                            </div>
                        </div>
                        <div class="field">
                            <div class="control">
                                <input name="sobrenome" id="sobrenome" class="input is-large" required="required" placeholder="Sobrenome"  maxlength="20" value="<?php echo $linha['sobrenome_usuario']; ?>">
                            </div>
                        </div>
                        <div class="field">
                            <div class="control">
                                <input name="email" type="email" class="input is-large" required="required" placeholder="E-mail"  maxlength="50" value="<?php echo $linha['email']; ?>">
                            </div>
                        </div>
                        <div class="field">
                            <div class="control">
                                <input name="empresa" class="input is-large" required="required" placeholder="Empresa" maxlength="30" value="<?php echo $linha['empresa']; ?>">
                            </div>
                        </div>
                        <div class="field">
                            <div class="control">
                                <input name="telefone" id="telefone" class="input is-large" required="required" placeholder="Telefone" maxlength="15" value="<?php echo $linha['telefone']; ?>">
                            </div>
                        </div>
                        <div class="field">
                            <div class="control">
                                <div class="select is-fullwidth select is-large">
                                    <select name="acesso" id="acesso" required="">
                                        <?php
                                        
                                        foreach ($acesso as $key => $value) {
                                        ?>
                                            <option value="<?php echo $value['id']; ?>">
                                                <?php echo $value['nivel']; ?>
                                            </option>
                                        <?php
                                        }   
                                        ?>

                                        <?php
                                        //exibe valor 0 do array no combobox caso acesso do usuário seja nível 0 (admin)
                                        if ($linha['acesso'] == '0') {
                                            $position = $acesso[0]
                                        ?>
                                            <option selected="" hidden="" value="<?php echo $position['id']; ?>">
                                                <?php echo $position['nivel']; ?>
                                            </option>

                                        <?php   
                                        }
                                        ?>

                                        <?php
                                        //exibe valor 1 do array no combobox caso acesso do usuário seja nível 1 (padrão)
                                        if ($linha['acesso'] == '1') {
                                            $position = $acesso[1]
                                        ?>
                                            <option selected="" hidden="" value="<?php echo $position['id']; ?>">
                                                <?php echo $position['nivel']; ?>
                                            </option>

                                        <?php   
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>   
                        </div>
                        
                        <button type="submit" name="submit" class="button is-success is-large">Salvar</button></form>
  • As you are a beginner, I recommend reading about handling events of the fields with JS, especially the event change. With this you will have a notion of how to solve your problem.

  • Thanks. I’ll do a little research

1 answer

0

If the need is to check that the user has only changed some field (even if it puts the same value), add the "disabled" attribute to the "button" and add the following Javascript code:

$("input, select, textarea").change(function(){
    $("button").removeAttr("disabled");
});

I recommend adding Ids or classes to your HTML tags.

Browser other questions tagged

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