Avoid double clicking and redirect to another page

Asked

Viewed 231 times

2

Good afternoon!
I made a function to prevent the user from pressing the same button twice, this part works perfectly.

global.js

$('.form-disable').on('submit', function(){

var self = $(this),
button = self.find('input[type="submit"], button'),
submitValue = button.data('submit-value');

button.attr('disabled','disabled').val((submitValue) ? submitValue : "Aguarde...");
});

The problem is that now it no longer redirects to the next page. The button is used to insert data into the database and to redirect the user.

php comment.

<form method="POST" action="" class="form-disable">
        <table width="80%" align="center" cellpadding="8">
            <tr>
                <td colspan="1" class="logo">
                    <img src="../classif/Imagens/logo.png" height="40" width="150" style="max-width: 100%; height: auto;">
                </td>
                <td class="opiniao" align="right">

                </td>
            </tr>
            <tr>
                <td colspan="3" class="pergunta" align="center">
                    <div class="responsive_pergunta">
                        <hr>
                        Deixe aqui o seu comentário
                        <hr>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <textarea rows="13" cols="165" name="textarea" id="textarea" placeholder=""></textarea>
                    <p class="num_restantes" style="text-align: right; color: #bfbfbf; padding-right: 80px;"><span id="caracteres_restantes">Limite de caracteres: 150</span></p>
                </td>
            </tr>
            <tr>    
                <td class="selecao">
                    <div class="cores">

                        <input type="submit" id="enviar" name="enviar" class="enviar" value="✓">
                    </div>
                    <?php
                    if (isset($_POST['enviar'])) {
                    //procura na tabela questionario o id da pergunta 'pergunta_id'
                        $procura = mysqli_query($link,"SELECT pergunta_id from questionario");
                        $registos_pergunta_id=mysqli_num_rows($procura);

                    //pegar pergunta_id da tabela questionario
                        while ($registos_pergunta_id!=0) {
                            $get_pergunta_id=mysqli_fetch_array($procura);
                            $registos_pergunta_id--;
                        }

                        $resposta = $_POST['textarea'];
                    //inserir os dados na tabela resposta
                        $inserir=mysqli_query($link,"INSERT INTO resposta (resposta_id, pergunta_id, resposta) VALUES ('','".$get_pergunta_id[0]."','$resposta')");
                        if (!$inserir) {
                        //caso os dados nao sejam inseridos na base de dados irá msotrar um erro a informar
                            echo "Erro ao inserir na base de dados";
                        }else
                        {
                        //se os dados forem inseridos na base de dados irá para a página agradecimento
                            header('location: agradecimento.php?id=1');
                        }
                    }
                    ?>
                    <td class="selecao" style="text-align: center;">
                        <div class="cores">
                            <input type="submit" name="desistir" class="desistir" value="X">
                            <?php
                            if (isset($_POST['desistir'])) 
                            {
                            //apagar o ultimo id inserido da tabela caso o utilizador desista da sua opçao
                                $apagar = mysqli_query($link,"delete from questionario order by pergunta_id desc limit 1");
                                if (!$apagar) {
                                    echo "Erro ao apagar o último registo inserido";
                                }else{
                                //redireciona novamente para a página principal
                                    header('location: index.php?id=1');
                                    exit();
                                }
                            }
                            ?>
                        </td>
                    </div>
                </td>
            </tr>   
        </table>
    </form>

Can someone help me? Thank you!

1 answer

1


Your Return false prevents the form from being sent, so simply remove it:

$('.form-disable').on('submit', function(){
 var self = $(this),
 button = self.find('input[type="submit"], button'),
 submitValue = button.data('submit-value'); 
 button.attr('disabled','disabled').val((submitValue) ? submitValue : "Aguarde...");
});

It is not being redirected because the headers have already been sent and you call the function again header() to make the redirect. You can resolve by redirecting in another way. With javascript, for example: in place of:

header('location: index.php?id=1');

Place:

echo "<script>window.location.href= 'index.php?id=1';";
  • Heheh, well that could be so simple! I have already tried to remove, what it does now is it updates the page only, does not redirect to the agradecimento.php?id=1

  • You can edit the question with the html code of your form?

  • I’ve already edited the question, I think that’s what you asked

  • I changed my answer

  • I tested but it did not work... he continues to autlizar the pages, but ready... I marked as right, because by its logic made a lot of sense!

  • In fact... without the function, the header no problem, works perfectly until, only when I add the function to disable the button, then no redirecting!

Show 1 more comment

Browser other questions tagged

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