2
I have a form
, and in this I have to inform the neighborhood, however the element that stores the neighborhood information may vary according to the value of the city selected previously.
Example:
If the city is "main" should change I should change the neighborhood countryside of a input
for a select
. I managed to make the trade like this:
HTML:
<div class="form-group has-feedback {{ $errors->has('bairro') ? 'has-error' : '' }}">
<label for="bairro" class="col-sm-2 control-label">Bairro</label>
<div id="bairro-select" class="col-sm-10" hidden>
<select class="form-control" name="bairro" id="bairro">
<option value="" selected>--- Escolha um bairro ---</option>
@foreach($bairros as $bairro)
<option value="{{ $bairro->nome }}">{{$bairro->nome}}</option>
@endforeach
</select>
</div>
<div id="bairro-input" class="col-sm-10">
<input type="text" class="form-control" name="bairro" id="bairro" placeholder="" minlength="3" maxlength="90">
</div>
</div>
Javascript:
$("#cidade").on('change', function() {
var cidade = $(this).find('option:selected').val();
if(cidade == 'Principal') {
bairroselect.style.display = "block";
bairroinput.style.display = "none";
}else {
bairroselect.style.display = "none";
bairroinput.style.display = "block";
}
});
But when will I send the form
in the request
the bairro
is always with the content of input
, even if selecting a bairro
in the select
this takes the value of input
.
The code I made leaves the div
just hides the element. How could you do so that only the value that is displayed is submitted?
You could edit the question and include the code of how you do the request, because each element has a
name
, how do you validate which one to use inPHP
?– Caique Romero
@Caiqueromero sorry I was doing a test and I didn’t even see it was the wrong code
– Mateus
The point is that the two have the same name, I made a Gambi that I send with different names and check in the controller which is not null, but I believe that there must be some better way
– Mateus
@Caiqueromero but then I won’t be able to use ?
– Mateus
See if my answer helps you.
– Caique Romero
Test however the code does not work to leave the field
disabled
– Mateus
I got it, the problem is that the id there was the
div
, I had to assign theselect
and ofinput
really, but thanks really helped a lot– Mateus