Exploding Value from Select Option Value

1k Views Asked by At

Now I'm working with Laravel 5.

I got value from select option like below:

select name="billing_provider"  onChange="changeLanguage(this.value)"> @foreach($tests as $test) 
option value="{{ $test->tax_id }},{{ $test->npi }}" name="billing_provider">
{{ $test->test_name }} </option>@endforeach </select>

Here I need to explode the option value and store it into another text field value.

How can I do that?

1

There are 1 best solutions below

0
On BEST ANSWER

First, you have made a mistake building your HTML. <select> has an attribute name which is going to be the key in the global arrays $_GET or $_POST. However <option> doesn't have attribute name. You should remove that.

If you want to separate the value of billing_provider in your backend (php /w Laravel), do so. In your method which handles the submission of the form:

$input = Input::get('billing_provider');
$billing_provider = explode(',', $input);
//Now $billing_provider[0] === $test->tax_id
//And $billing_provider[1] === $test->npi

Or if you want to perform this action before form submission and you're using jQuery, then you can do this: https://jsfiddle.net/17fkpqbe/

JavaScript (/w jQuery):

$(document).ready(function() {
    $('select').change(function() {
        $('.inputs').html('');
        var $explodedVal = $(this).val().split(',');
        for(i = 0; i < $explodedVal.length; i++) {
            $('.inputs').append('<input value="' + $explodedVal[i]+'">');
        }
    });
});

HTML:

<select>
    <option></option>
    <option value="123,555">Some Name 1</option>
    <option value="123,444">Some Name 2</option>
    <option value="123,333,555">Some Name 3</option>
</select>
<div class="inputs"></div>