$ _post returns empty, I tried everything, guys. Please help me.

0

I have this problem and I can not solve it, the form is correct but when the user clicks the save button, the file save.php is triggered normally, but loses the contents of $ _POST .... Help me! p>

index.php file

<!DOCTYPE html>
<html lang="pt" class="no-js">    
<body>
    <div>
        <div>
            <form action="salvar.php" method="post">
                <div class="row">                
                    <div class="form-group">
                        <label for="name">Id</label>
                        <input type="text" class="form-control" name="id" value=""> 
                    </div>
                    <div class="form-group">
                        <label for="name">Código</label>
                        <input type="text" class="form-control" name="codigo" value=""> 
                    </div>
                    <div class="form-group">
                        <label for="name">Descrição</label>
                        <input type="text" class="form-control" name="descricao" value=""> 
                    </div>
                </div> 
            </form>
        </div>
        <div>
            <button type="submit" onclick="salvar()">Salvar</button>
            <button type="button" onclick="fechar()">Cancelar</button>
        </div> 
    </div>
    <script type="text/javascript">
        function salvar() {
            window.location.href = "salvar.php";
        }
    </script>  
</body>
</html>

In the file save.php I read the variable $ _post and the value is empty, as I show in the attached file.

save.php file

<!DOCTYPE html>
<html lang="pt" class="no-js">    
    <body>
        <?php 
            var_dump($_POST);
        ?>
    </body>
</html>

Result:

C: \ wamp \ www \ save.php: 5: array (size = 0)   empty

I can not solve it and I have already lost the whole Sunday. I did not change version of anything, no update. Good week to all and thank you for wasting your time.

    
asked by anonymous 16.07.2018 / 03:15

2 answers

1

The way you are doing is as if you were redirecting to the save.php, the ideal would be to use ajax to retrieve this data, or using jquery or any other http client, such as axios for example.

Try doing something like this, not forgetting to call jquery.

<!DOCTYPE html>
    <html lang="pt" class="no-js">    
    <body>
        <div>
            <div>
                <form action="salvar.php" id="form_exemplo">
                    <div class="row">                
                        <div class="form-group">
                            <label for="name">Id</label>
                            <input type="text" class="form-control" name="id" value=""> 
                        </div>
                        <div class="form-group">
                            <label for="name">Código</label>
                            <input type="text" class="form-control" name="codigo" value=""> 
                        </div>
                        <div class="form-group">
                            <label for="name">Descrição</label>
                            <input type="text" class="form-control" name="descricao" value=""> 
                        </div>
                    </div> 
                </form>
            </div>
            <div>
                <button id="btn_salvar">Salvar</button>
                <button type="button">Cancelar</button>
            </div> 
        </div>
        <script type="text/javascript">
            $(document).ready({
               $('#btn_salvar').on('click', function(event){
                    event.preventDefault();

                    $.ajax({
                        url:'salvar.php',
                        type:'post',
                        data:$('#form_exemplo').serialize(),
                        success:function(response){
                            console.log(response);
                        }
                    })
               });
             })
        </script>  
    </body>
    </html>
    
16.07.2018 / 03:34
3

The window.location.href only redirects the page, it does not send the form. Ideally, you should put the button inside form , but if you want to call a function for it, you can submit the form with the submit() method:

function salvar() {
   document.forms[0].submit();
}

You can remove the type="submit" of the button because it makes no difference if you are using the onclick event:

<button onclick="salvar()">Salvar</button>
    
16.07.2018 / 03:32