Retrieve data from a form

1

Maybe what I'm asking is something very obvious, but as I'm learning I prefer to consult. When I have a form with all the complete data and send me an error message, the input fields bring back the written data to not write it again, but in the combobox the same thing does not happen to me, as I do to rescue the Selected option.

I have the following code

<select name="categoria" id="categoria">
        <option value=""></option>
            <?php
                if($data!=null) {
                    foreach ($data->data as $key => $value) {
                        if($value->Id==$pdata['categoria']){  
                            echo '<option selected value="'.$value->Id .'">' . $value->Label . '</option>';
                        }else{
                            echo '<option  value="'.$value->Id .'">' . $value->Label . '</option>';
                        } 

                    }
                }else{
                    echo '<option value="">--</option>';
                }


            ?>
        </select>

<select name="subcat" id="subcat">
            <option value="">--</option>
        </select>

<script>
$(document).ready(function (){
  $('#categoria').change(function (){ 
        getSubject($(this).val());
    });
    function getSubcat(valor){
        $.getJSON( "getSubcat.php", { sub: valor }, function( data ) {
            var items = [];
            $('#subcat').find('option').not(':first').remove();
            $.each( data['Subcat'], function( key, val ) {
                $('#subcat').append($('<option>', {
                    value: val.Subcat,
                    text : val.Subcat
                }));
            });
        });
    }
    <?php 
        if($pdata['categoria']!=""){
            echo "getSubcat(".$pdata['categoria'].");";
        }
    ?>
});

Please, if you can help me. Thanks

    
asked by Karli 20.06.2018 в 17:48
source

2 answers

1

I see that you use laravel so it is easier for you to use the function withInput , for more reference I leave you the link of the documentation Here . Below I show you an example line in the controller:

$validator=Validator::make($request->all(), [
                'input' => 'required'
            ]);

            if ($validator->fails()) {
                Flash::error('Debes subir un archivo de tipo imagen.')->important();
                return redirect(route('create_public'))->withInput();
            }
    
answered by 20.06.2018 / 23:28
source
1

If what you want to do is stay in the selected option once you submit the submit here's the example:

<select name="categoria" id="categoria">
        <option value=""></option>
            <?php
                if($data!=null) {
                    foreach ($data->data as $key => $value) {

                        if($value->Id == $_POST['categoria']){  
                        echo '<option selected value="'.$value->Id .'">' . $value->Label . '</option>';
                        }else{
                         echo '<option  value="'.$value->Id .'">' . $value->Label . '</option>';
                        }    

                    }
                }else{
                    echo '<option value="">--</option>';
                }


            ?>
        </select>

Note: I am using the global variable $ _ POST to capture the previously selected value. I am placing it in a if , in which I am saying yes, the value of $ value- > Id is equal to the value captured in the form show me later of the submit .

    
answered by 20.06.2018 в 18:00