Pegar dados de radio button e input
Olá pessoal, estou tentando pegar os dados de radio buttons e de inputs de um formulário para salvar no banco.
Os radio são de pessoa fisica e juridica, então quando seleciona o radio pessoa fisica ele mostra na tela os inputs de rg e cpf.
E quando seleciona o radio pessoa juridica ele mostra os inputs de cnpj, i.e, etc...
Mas quando preencho e dou
var_dump($_POST);
ele retorna um array que não estou conseguindo percorrer :/
Não manjo muito de javascript, jquery, etc.. então o código que mostra os inputs de acordo com o radio selecionado eu peguei na net, então se acharem melhor alterar o html/js poderiam me ajudar.
Segue o código:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
var_dump($_POST);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style type="text/css">
input.form-control {
display: none;
}
</style>
</head>
<body>
<form method="POST">
<div class="radio">
<label>
<input type="radio" name="custom_field[account][1]" name="pf" id="id-custom_field-account-1-1" value="1"> Pessoa Física
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="custom_field[account][1]" name="cnpj" id="id-custom_field-account-1-2" value="2"> Pessoa Jurídica
</label>
</div>
<input type="text" name="custom_field[account][4]" name="razaosocial" value="" placeholder="Razão Social" id="input-custom-field4" class="form-control" vk_1bc56="subscribed">
<input type="text" name="custom_field[account][5]" name="cnpj" value="" placeholder="CNPJ" id="input-custom-field5" class="form-control" vk_1bc56="subscribed">
<input type="text" name="custom_field[account][6]" name="ie" value="" placeholder="I.E" id="input-custom-field6" class="form-control" vk_1bc56="subscribed">
<input type="text" name="custom_field[account][3]" name="rg" value="" placeholder="RG" id="input-custom-field3" class="form-control" vk_1bc56="subscribed">
<input type="text" name="custom_field[account][2]" name="cpf" value="" placeholder="CPF" id="input-custom-field2" class="form-control" vk_1bc56="subscribed">
<input type="submit" value="enviar">
</form>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('input:radio[name="custom_field[account][1]"]').on("change", function() {
if (this.checked && this.value == '1') {
$("#input-custom-field2, #input-custom-field3").show();
$("#input-custom-field4, #input-custom-field5, #input-custom-field6").hide();
} else {
$("#input-custom-field4, #input-custom-field5, #input-custom-field6").show();
$("#input-custom-field2, #input-custom-field3").hide();
}
});
});
</script>
</body>
</html>
Do jeito que esta, o var_dump mostra isso:
array (size=1)
'custom_field' =>
array (size=1)
'account' =>
array (size=6)
1 => string '2' (length=1)
4 => string '2232' (length=4)
5 => string '232' (length=3)
6 => string '23' (length=2)
3 => string '' (length=0)
2 => string '' (length=0)
Desde já agradeçoDiscussão (1)
Carregando comentários...