mercredi 30 décembre 2015

Ajax combo box in laravel 5

I have a problem in implementing AJAX in Laravel 5 with blade syntax. I have 2 simple dropdowns. For example dropdown A and dropdown B. If dropdown A value is 1, I want the dropdown list in B to be Apple and Avocado. If dropdown A value changed to 2, the dropdown list in B will be Banana and Blueberry. I have no problem when doing it with just PHP and AJAX. Here is my code :

in main.php

<script type='text/javascript'>
        $(function(){
            $('#alphabet').change(function(){
                console.log($(this));
                $.get( 'fruit.php' , { option : $(this).val() } , function ( data ) {
                    $ ( '#fruit' ) . html ( data ) ;
                } ) ;
            });
        });
    </script>

    <div class='form-group'>
        <label class='control-label col-sm-4'>Alphabet</label>
        <div class='col-sm-6'>
            <div class='dropdown'>
                <select style='width: 261px' class='form-control' name='alphabet' id='alphabet'>
                    <option value='0' selected='' >-Choose-</option>
                    <option value='1'> 1 </option>
                    <option value='2'> 2 </option>
                </select>
            </div>
        </div>
    </div>

    <div class='form-group'>
        <label class='control-label col-sm-4'>Fruit</label>
        <div class='col-sm-6'>
            <div class='dropdown'>
                <select style='width: 261px' class='form-control' name='fruit' id='fruit'>
                    <option value='0' selected='' >-Choose-</option>
                </select>
            </div>
        </div>
    </div>

in fruit.php

<?php
$Options = Array ( 
    1 => Array ( 
        'Apple' ,
        'Avocado'
    ) , 
    2 => Array ( 
        'Banana' ,
        'Blueberry'
    )

) ; 

forEach ( $Options [ $_GET [ 'option' ] ] as $Item ) {
    printf ( '<option value="%s">%s</option>' , $Item , $Item ) ;
}

How can I implement this on Laravel 5?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire