Value Radio Button does not pass via php Post

Asked

Viewed 956 times

1

Only two radio button.

$faturamento_tipo_post = $_POST['faturamento_tipo'];
if (isset($faturamento_tipo_post) && $faturamento_tipo_post == "cl") {
$tipo_faturamento = 1;
}
elseif (isset($faturamento_tipo_post) && $faturamento_tipo_post == "co") {
$tipo_faturamento = 0;
}
else
 {
$tipo_faturamento = "Houve um erro na busca do faturamento tipo";
echo '<br>'.$tipo_faturamento.'<br>';

exit;
}


    <label class="radio">
    <input type="radio" name="faturamento_tipo" value="cl"  />
    Cliente
</label>
<label class="radio">
    <input type="radio" name="faturamento_tipo" value="co"  />
    Cortesia
</label>

It doesn’t work, I always get the last message, the variable is empty.

  • Post the form code

  • 1

    Gives a print_r($_POST); to see what appears.. if comes the returns.

  • That’s exactly what I did, and I don’t get anything back from the radios

  • 1

    Are inside a <form> ?

  • Place the <form> line to see if nothing was missing from it.

2 answers

1


I worked as an example: I believe this will solve your problem. However, make sure $_POST is coming correctly from your form.

PHP code

#Verificação do Tipo de Faturamento
function verificaTipo($tipo){

    switch($tipo){

        default: 
            $tipo_faturamento = "Nenhum tipo";
            break;

        case 'cl':
            $tipo_faturamento = 1;
            break;

        case 'co':
            $tipo_faturamento = '0';
            break;

    }

    #Retorno
    return $tipo_faturamento;


}


#Saida para o HTML  
if(!empty($_POST))
echo verificaTipo($_POST['faturamento_tipo']);

Form

<form enctype="multipart/form-data" action="<? echo $PHP_SELF;?>" name="formulario" method="post">
    <label class="radio">
        <input type="radio" name="faturamento_tipo" value="cl"  />
            Cliente
        </label>
    <label class="radio">
        <input type="radio" name="faturamento_tipo" value="co"  />
            Cortesia
        </label>
        <input type="submit" value="Enviar">
</form>
  • I miss the method which is omitted is get by default. P

  • I changed it, and it also includes a new line, and I tried it here on my localhost to see, and it worked. : ) @rray Thanks for the tip.

  • @Andrébaill tested with your function, do not print me anything, my <form> was exactly the same as your example, what can be this?

  • Post your complete form, with tags and etc... So we can analyze too.

  • 1

    Check if the form method is included: method="post"

  • Yeah, I’m the form was working normally, I don’t know what happened

  • Did you manage to check if the post is coming? Or at least if you are entering the function I indicated to you?

  • I solved, the problem however incredible it seems, were div that were with closure </div>, inside the <form>, but div were opened before the form.

Show 4 more comments

1

Clear the information coming from the form:

$faturamento_tipo_post = trim(strip_tags($_POST['faturamento_tipo']));

Sometimes white spaces and or html tags may come.

Trim: removes whitespace both left and right of the string.

strip_tags: removes the html tags.

Browser other questions tagged

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