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

Asked

Viewed 105 times

0

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

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 save.php file I read the $_post variable and the value is empty, as shown in the attached file.

php save file

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

Upshot:

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

I can’t fix it, and I’ve already missed the whole Sunday. I didn’t change the version of anything, no update. Good week everyone and thank you for taking your time.

  • You want to use Ajax for this?

2 answers

3

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

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

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

<button onclick="salvar()">Salvar</button>
  • 1

    Very good. Thank you. Few people want to help beginners like me, few have patience with simple questions, but for us it is an answer as yours is of great value.Solved!

1


The way you are doing it is as if you were redirecting to save it.php, the ideal would be to use ajax to recover this data, or using jquery or any other http client, like 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>
  • Solved! I joined the two answers and solved my problem here. Thank you very much saw Alexandre. One day I will be the person who responds to beginners if God willing. Thank you and good week!

  • Why would it be ideal to use Ajax? I don’t understand.

Browser other questions tagged

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