PHP获取选择表单的选定值 [英] PHP get selected value of select form

查看:168
本文介绍了PHP获取选择表单的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Laravel,目前正在创建一个表单,用户可以先选择一个类别,然后选择一个相关的子类别。



类别有一个id和一个名称,一个子类别有一个ID和一个名字,以及一个parent_id(这是父类别的ID)。

现在我尝试以下操作:
用户可以选择一个类别,第二个选择出现,并且用户可以从属于父类别的子类别中选择子类别(仅限于那些!)。

因此,该视图获取所有类别和然后,对于我这样做的类别:

 <$> 

c $ c>< div class =form-group {{$ errors-> has('category')?'has-error':''}}>
< label for =categoryclass =col-md-4 control-label> Kategorie< / label>

< div class =col-md-6>
< select>
@foreach($ categories as $ category)
< option value =<?php echo $ category ['id'];?>> {{$ category-> name }}< /选项>
@endforeach
< / select>
< / div>
< / div>

对于子类别,我只有子类别:

 < div class =form-group {{$ errors-> has('subCategory')?'has-error':''}} > 
< label for =subCategoryclass =col-md-4 control-label> Unterkategorie< / label>

< div class =col-md-6>
< select>
@foreach($ subCategories as $ subCategory)
< option value =<?php echo $ subCategory ['id'];?>> {{$ subCategory-> name }}< /选项>
@endforeach
< / select>
< / div>
< / div>

现在问题是:我需要做的是(在子类foreach中) @if 并检查类别选择的选项的值然后检查 if($ subcategory-> parent_id == $ chosenOptionID),以伪代码表示。我怎样才能做到这一点?我不希望发送表单或其他东西,在有人没有选择子类别之前......当然,我也许可以在选择类别时向控制器发送ajax请求,然后检查控制器中的选项并发回该ID。但是这会造成很大的流量,并且可能不会是最好的解决方案,所以我只想这样做,如果我没有更好的方法去做....



PS:问题不在于选择类别时隐藏子类别,那是基本的js,我知道该怎么做。我只想知道,如果有可能读取类别的值选择而表单尚未发送。



编辑:我现在尝试了AJAX,但似乎有一点小错误...
我改变了什么:

 < div class =form-group {{$ errors-> has('subCategory')?'has-error':''}}> 
< label for =subCategoryclass =col-md-4 control-label> Unterkategorie< / label>

< div class =col-md-6>

< / select>
< / div>
< / div>

select现在是空的并且有一个ID。然后在同一个文件中,我有以下JS:

 < script> 
$()。ready(function(){
$('#categorySelect')。change(function(){
var selectedOption = $('#categorySelect')。find( :选择)。val();
$ .ajax({
url:'/ getSubCategories',
data:{'id':selectedOption},
type:' GET',
success:function(data){
$('#subCategorySelect')。html(data);
}
});
});
});
< / script>

/ getSubcategories route指向以下函数在我的控制器中:

  public function returnSubCategories(Request $ request){
$ subCategories = Subcategory :: where(' parent_id','=',$ request-> id) - > get();
foreach($ subCategories as $ subCategory){
echo< option value ='$ subCategory-> id'> $ subCategory-> name< / option>;
}
}


I'm using Laravel and currently creating a form, where users can first choose a category, and then choose a relating subcategory.

A category has an id and a name, a subcategory has an id and a name, as well as a parent_id (so that's the id of the parent category).

Now I try the following: The user can pick a category, after he did that, the second select appears and the user can choose the subcategory from the subcategories that belong to the parent category (and only those!).

Therefore, the view gets all categories and all subcategories from the controller.

Then, for the categories I did this:

<div class="form-group{{ $errors->has('category') ? ' has-error' : '' }}">
                                <label for="category" class="col-md-4 control-label">Kategorie</label>

                                <div class="col-md-6">
                                    <select>
                                        @foreach($categories as $category)
                                            <option value="<?php echo $category['id'];?>">{{$category->name}}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>

And for the subcategories I have the same, just with subcategories:

<div class="form-group{{ $errors->has('subCategory') ? ' has-error' : '' }}">
                                <label for="subCategory" class="col-md-4 control-label">Unterkategorie</label>

                                <div class="col-md-6">
                                    <select>
                                        @foreach($subCategories as $subCategory)
                                            <option value="<?php echo $subCategory['id'];?>">{{$subCategory->name}}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>

Now the question is: What I need to do is (in the subcategory foreach) a @if and check for the value of the chosen option of the category select and then check if($subcategory->parent_id == $chosenOptionID), to say it in pseudo code. How can I do this? I don't want to send the form or something, before somebody didn't choose the subcategory as well.... Of course, I maybe could send an ajax request to the controller whenever a category is chosen, then check the value of the option in the controller and send back the id. But this would cause very much traffic and would propably not be the best solution, so I'd like to do this only, if I don't get a better way to do....

PS: The question is not about hiding the subcategories when no category is chosen, thats basic js, I know how to do that. I only want to know, if it is possible to read the value of the category select while form is not sent yet.

EDIT: I now tried the AJAX thing but there seems to be a little error... What I changed:

<div class="form-group{{ $errors->has('subCategory') ? ' has-error' : '' }}">
                                <label for="subCategory" class="col-md-4 control-label">Unterkategorie</label>

                                <div class="col-md-6">
                                    <select class="form-control" id="subCategorySelect">

                                    </select>
                                </div>
                            </div>

The select is now empty and has an ID. Then in the same file I have the following JS:

<script>
    $().ready(function(){
        $('#categorySelect').change(function(){
            var selectedOption = $('#categorySelect').find(":selected").val();
            $.ajax({
                url: '/getSubCategories',
                data: {'id': selectedOption},
                type: 'GET',
                success: function (data) {
                    $('#subCategorySelect').html(data);
                }
            });
        });
    });
</script>

/getSubcategoriesroute points to the following function in my controller:

public function returnSubCategories(Request $request){
        $subCategories = Subcategory::where('parent_id', '=', $request->id);
        foreach($subCategories as $subCategory){
            echo "<option value='$subCategory->id'>$subCategory->name</option>";
        }
    }

But it doesn't work.. The ajax request is defintely sent on selecting an option, as well the correct id is sent. But somehow nothing is outputted... Any ideas why?

解决方案

I finally got it working with AJAX. This is my initial subCategorySelect.

<div class="form-group{{ $errors->has('subCategory') ? ' has-error' : '' }}">
                                <label for="subCategory" class="col-md-4 control-label">Unterkategorie</label>

                                <div class="col-md-6">
                                    <select class="form-control" id="subCategorySelect">

                                    </select>
                                </div>
                            </div>

The select is now empty and has an ID. Then in the same file I have the following JS:

<script>
    $().ready(function(){
        $('#categorySelect').change(function(){
            var selectedOption = $('#categorySelect').find(":selected").val();
            $.ajax({
                url: '/getSubCategories',
                data: {'id': selectedOption},
                type: 'GET',
                success: function (data) {
                    $('#subCategorySelect').html(data);
                }
            });
        });
    });
</script>

/getSubcategoriesroute points to the following function in my controller:

public function returnSubCategories(Request $request){
        $subCategories = Subcategory::where('parent_id', '=', $request->id)->get();
        foreach($subCategories as $subCategory){
            echo "<option value='$subCategory->id'>$subCategory->name</option>";
        }
    }

And it works perfectly.

这篇关于PHP获取选择表单的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆