When submitting form, GET data is lost

Asked

Viewed 94 times

0

I’m trying to upload the data from a form to the database. I have data coming from POST and also from GET, but when clicking on the Submit button the value of the variable that receives data from GET is reset.

THIS IS THE FORM.

<form action="pedidos.php" method="post">

<table border="1">

        <tr>
        <td> NOME DO CLIENTE  </td>
        <td> <?php echo strtoupper($nome_cliente); ?> </td>
        </tr>

        <tr>
        <td> PRODUTO: </td>
        <td> <select name="produto">
        <?php 
            if(isset($selecionar_produto) && mysql_num_rows($selecionar_produto)){
                while($produto = mysql_fetch_array($selecionar_produto)){ ?>
                <option value="<?php echo $produto["id_produto"]; ?>"> <?php echo $produto["nome_produto"]; ?> </option>
        <?php
                }
            }
        ?>  

        <tr>
        <td> QUANTIDADE: </td>
        <td> <input type="number" name="qtde"> </td>
        </tr>

        <tr>
        <td> RUA: </td>
        <td> <?php echo strtoupper($rua); ?> </td>
        </tr>

        <tr>
        <td> NÚMERO </td>
        <td> <?php echo strtoupper($num); ?> </td>
        </tr>

        <tr>
        <td> BAIRRO </td>
        <td> <?php echo strtoupper($bairro); ?> </td>
        </tr>

        <tr>
        <td> CIDADE: </td>
        <td> <?php echo strtoupper($cidade); ?> </td>
        </tr>

        <tr>
        <td> STATUS </td>
        <td> 
            <select name="status">
                <?php
                    if(isset($selecionar_status) && mysql_num_rows($selecionar_status)){
                        while($status = mysql_fetch_array($selecionar_status)){ ?>
                        <option value="<?php echo $status["id_status"];?>"> <?php echo $status["status"]; ?> </option>
                <?php
                        }
                    }
                ?>
        </td>
        </tr>

        <tr>
        <td colspan="2"> <input type="submit" name="botao" value="Cadastrar"> 
        <a href="painel.php"> <input type="button" name="botao" value="Voltar"> </a>

        </td>
        </tr>               

            </select>       
        </td>
        </tr>



    </table>
</form>

THIS IS PHP TO REGISTER THE REQUEST

   if (isset($_POST["botao"]) && $_POST["botao"] == "Cadastrar"){   

        $gravar_pedidos = mysql_query("INSERT INTO pedidos(id_cliente,id_produto,id_status) values ('$id_cliente','$produto','$status')");


        if(!$gravar_pedidos){
            echo "Erro ".mysql_error();
        }else{
            echo "<meta http-equiv='refresh' content='0;URL=painel.php'/>

            <script type=\"text/javascript\">
            alert(\"Operação Realizada com sucesso!!\");
            </script> ";                
        }   

}
  • edit the question by placing the html form code so that we can see how it is and see if the problem consists of the form or your script. As soon as you edit the question, you comment here and I take a look and see what I can help you with.

  • $id_cliente = (isset($_GET["id_cliente"])); is not correct. Since you will only be with true or false. What you want to do is $id_cliente = ($_GET["id_cliente"]);

  • Copy and paste into your question how is the html of your form, and when you mix get and post I think it is best to use the method that recovers both $_REQUEST['xxx']. [ http://php.net/manual/en/reserved.variables.request.php ]

  • how do I use $_REQUEST? because I’m using the post and get

  • If any answer fits, mark it as accepted. See how and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

2 answers

0


Corrections to be made:

1 - Hit the line html PRODUTO, that is, close the select, the td and the tr

    <tr>
    <td> PRODUTO: </td>
    <td> <select name="produto">
    <?php 
        if(isset($selecionar_produto) && mysql_num_rows($selecionar_produto)){
            while($produto = mysql_fetch_array($selecionar_produto)){ ?>
            <option value="<?php echo $produto["id_produto"]; ?>"> <?php echo $produto["nome_produto"]; ?> </option>
    <?php
            }
        }
    ?>
</select></td></tr>

2 - Missing to recover the values sent from the form:

    if (isset($_POST["botao"]) && $_POST["botao"] == "Cadastrar"){  

        $id_cliente = $_POST['id_cliente'];
        $produto = $_POST['produto'];
        $status = $_POST['status']; 

        $gravar_pedidos = mysql_query("INSERT INTO pedidos(id_cliente,id_produto,id_status) values ('$id_cliente','$produto','$status')");
         ......................

Note: The form lacks a field with values of the id_client and the name="id_client"

You can replace POST with GET. Where there is the word POST replace with GET ,ie,

<form action="pedidos.php" method="GET">

if (isset($_GET["botao"]) && $_GET["botao"] == "Cadastrar"){ 
    $id_cliente = $_GET['id_cliente'];
    $produto = $_GET['produto'];
    $status = $_GET['status']; 

The most advised is to use POST


About your question

"how do I use $_REQUEST? because I’m using the post and get"

The $_REQUEST is the generic type of $_GET, $_POST, whether your data is coming via $_GET or $_POST, rescues both.

if (isset($_REQUEST["botao"]) && $_REQUEST["botao"] == "Cadastrar"){ 
   $id_cliente = $_REQUEST['id_cliente'];
   $produto = $_REQUEST['produto'];
   $status = $_REQUEST['status']; 

In that case your form can both be

<form action="pedidos.php" method="post">

how much

<form action="pedidos.php" method="GET">

Its use is little recommended, learn more in Use of $_REQUEST instead of $_GET, $_POST and $_COOKIE

  • Thanks Leo was just that, our helped me a lot.

  • From what I could see it was a lot of lack of attention

0

sol25lua First check the attribute values name of your form are the same as the value of the super global POST.

Another suggestion would be to assign the POST values before the block if(and without the function isset() and inside the if block you could continue checking if your id is "set" and from there perform the Insert in the database.

Browser other questions tagged

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