Request with Checkbox + inputys

Asked

Viewed 83 times

0

Guys, how would p/ make a mini ordering system, through checkbox and input, this would be the idea in the code below:

<form method="POST">
    <input type="checkbox" name="produto[]" value="Feijao">Feijao - Quantidade
    <input type="number" name="qtd[]" min="0" max="99">


    <input type="checkbox" name="produto[]" value="Arroz">Arroz - Quantidade
    <input type="number" name="qtd[]" min="0" max="99">

    <input type="submit" name="">
</form>

how would p/ pick up these items via php? the idea would be: Quantity and product marked next - Ex:

2 - Feijão
3 - Arroz
  • The problem is getting the values using PHP $_POST?

  • Yes friend......

1 answer

1


Form use indexes in inputs Names

bean: name="produto[0]" name="qtd[0]"

for rice: name="produto[1]" name="qtd[1]"

<form method="POST" action="">
    <input type="checkbox" name="produto[0]" value="Feijao">Feijao - Quantidade
    <input type="number" name="qtd[0]" min="0" max="99">


    <input type="checkbox" name="produto[1]" value="Arroz">Arroz - Quantidade
    <input type="number" name="qtd[1]" min="0" max="99">


    <input type="submit" name="enviar">
</form>

PHP

  1. Loop-free

    if (isset($_POST['enviar'])){
        $feijao = $_POST['produto']['0'];
        $qtdfeijao = $_POST['qtd']['0'];
        $arroz = $_POST['produto']['1'];
        $qtdarroz = $_POST['qtd']['1'];
    
        if($feijao!="" && $qtdfeijao!=""){
            echo $qtdfeijao." - ".$feijao;
        }
    
        echo "<br>";
    
        if($arroz!="" && $qtdarroz!=""){
            echo $qtdarroz." - ".$arroz;
        }
    }
    
  2. looped

    if(isset($_POST['enviar'])){
        // quantidade de checkboxes 2
        for ($i=0;$i<2;$i++) {
            $Prod = $_POST['produto'][$i];
            $Quant = $_POST['qtd'][$i]; 
            if ($Prod!="" && $Quant!=""){
                echo $Quant.' - '.$Prod.'<br />';   
            }
        }
    
    }       
    
  • Brother is working almost 100%, but when I select only the rice, give error =/

  • Congratulations friend, that’s right... Fight for the strength!

Browser other questions tagged

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