kendrick's avatar

Change input, based on select:option

I have a Verification.php Model, with a verification_table:id, name, city, country

I want to put the select option below, before a signup form. After the user selected a verification, we need to parse the values of $verification into the signup form.

<select> 
        <option value="0">Select your information</option>
                @foreach(App\Models\Verification::all() as $verification)
                    <option value="{{$verification->id}}">{{$verification->city}}</option> 
                @endforeach 
</select> 

How can one build a bridge from the select option to the form input?

@if(select != 0)

put $verification->city into the input #ver-city

@else

let user type its information

@endif 

Signup:

<form>
<div>
<input type="text" name="city" value="{{ Request::old('city') ?: ''}}" id="ver-city" placeholder="{{__('auth.partner-fn-placeholder')}}">
              

@if ($errors->has('city'))
<span>{{ $errors->first('city') }}</span>
@endif
</div>
</form>

The process should help pre-validate upcoming users. Any ideas how to provide the bridge?

Kindest Regards

0 likes
11 replies
lostdreamer_nl's avatar
Level 53

if you have included jQuery it should be as easy as:

// first, give your select box an ID
<select id="myselect"> 
...
</select>


// add the following onChange script
<script>
$('#myselect').change(function() {
    var value = $(this).val();
    if(!value) {
        // empty value and make the input editable
        $("#ver-city").val("").prop('readonly', false);
        return;
    }
    // set the value and make sure the user cannot edit it
    var name = $("option:selected", this).text();
    $("#ver-city").val(name).prop('readonly', true);
});
</script>
1 like
kendrick's avatar

Hey @lostdreamer_nl

Thank you for your help.

jQuery is included. Also working with a select2 option.

Somehow it won't input the option:selected. Maybe it can't grab the values from the Verification Model. How could I console.log if it triggers the input?

kendrick's avatar

My fault, had a typo on the select #id.

It works.

How can I now grab values for the other inputs based on the select option? @lostdreamer_nl


var name = $("option:selected", this).text();
$("#ver-name").val(name).prop('readonly', true); // {{$verification->name}}
...

lostdreamer_nl's avatar

in that case, you'd need to have the data in json format in JS as well:

// controller
$verifications = \App\Models\Verification::all();
return view('my-view', ['verifications' => $verifications])
// view
<select> 
        <option value="0">Select your information</option>
                @foreach($verifications as $verification)
                    <option value="{{$verification->id}}">{{$verification->city}}</option> 
                @endforeach 
</select> 

...


<script>
var verifications = {!! $verifications->keyBy('id')->toJson() !}};


$('#myselect').change(function() {
    var value = $(this).val();
    if(!value) {
        // empty value and make the input editable
        $("#ver-city").val("").prop('readonly', false);
        return;
    }

    // grab the Model data for this ID from the array of verifications:
    var model = verifications[value];
    console.log(model);
    // set the value and make sure the user cannot edit it
    $("#ver-city").val(model.name).prop('readonly', true);
    // fill other fields you need with model.someProperty
});

</script>
1 like
kendrick's avatar

Great, @lostdreamer_nl

Thanks so much.

It works, with the little changes.

var verifications = {!! $verifications->get()->keyBy('id')->toJson() !!};
lostdreamer_nl's avatar

calling get() again should not be needed (it will run another MySQL query)

If you have the code in your controller setup as I stated, you should already have a Collection in $verifications on which you can call keyBy()

saving you from doing another (needless) query.

kendrick's avatar

Without get(), I get:

Illuminate\Database\Query\Builder::keyBy does not exist.

lostdreamer_nl's avatar

in that case, there should be something off in the controller:

$verifications = \App\Models\Verification::all();
return view('my-view', ['verifications' => $verifications])

$verifications should be a collection when it's send to the view, not a builder (query) object.

It could also be that you are looping over something in your view and overriding the $verifications variable

1 like
kendrick's avatar

I think it is because I am looping over something in my view, as there are multiple selects, for multiple Models. @lostdreamer_nl

Thank you for your great help.

lostdreamer_nl's avatar

for your own sake, try not to re-use variables ;) specially in loops, it will give strange problems that are pretty hard to debug later.

Please or to participate in this conversation.