简体   繁体   English

Laravel 表单:如何在不使用表单的情况下将隐藏信息传递给 controller

[英]Laravel Form: How to pass hidden information to controller without using form

I'm creating a website with a catalog, trading system, and custom currency (In Laravel).我正在创建一个包含目录、交易系统和自定义货币的网站(在 Laravel 中)。

I have the catalog and custom currency completely done (at-least to far), it's just the trading system.我已经完全完成了目录和自定义货币(至少到目前为止),它只是交易系统。 I'm so close to having the trading system done, except for this one thing that keeps holding me back, even though I think it should be pretty simple to do.非常接近完成交易系统,除了件事一直阻碍我,尽管我认为它应该很简单。

Each catalog item has a unique ID (in the database called uid), and the trading system Trades items based on the unique item id.每个目录项目都有一个唯一的 ID(在数据库中称为 uid),交易系统根据唯一的项目 ID 交易项目。

Here is the Trading Page for reference.这是交易页面供参考。 I have it to where if you click the checkbox on an item you want or want to give away, it sends all the uid's you checked into one array ( Note: I have 2 seperate arrays, one for the offering items, and one for the requesting items ).我有它,如果您单击您想要或想要赠送的项目上的复选框,它会将您检查的所有 uid 发送到一个数组中(注意:我有 2 个单独的 arrays,一个用于提供项目,一个用于请求项目)。

But the way I have it right now is that it gets each Item uid based off a form input.但是我现在拥有它的方式是它根据表单输入获取每个 Item uid。 Since I don't want users being able to edit items unique ID, I need a way to do this in the backend.由于我不希望用户能够编辑项目的唯一 ID,因此我需要一种在后端执行此操作的方法。 I've tried researching but I haven't gotten too far with that.我已经尝试过研究,但我还没有走得太远。

I'm kind of new to laravel and making websites as a whole, so I don't really know how to go forward with this.我对 laravel 和作为一个整体制作网站有点陌生,所以我真的不知道如何 go 向前推进。 I'm thinking I will have to use JavaScript, but I don't know how to send information to the controller using javascript.我想我将不得不使用 JavaScript,但我不知道如何使用 javascript 将信息发送到 controller。 I've also heard encoding then decoding the values but I have no idea how to do that either.我也听说过编码然后解码值,但我也不知道该怎么做。

HTML Code (Offering Side): HTML 代码(提供方):

@foreach (Auth::user()->inventory()->paginate(9999) as $itemb)
                        <form class="form-horizontal" method="POST" enctype="multipart/form-data" action="{{ route('trade.s', $user->id, [$itemb->uid]) }}">
                        {{ Form::token() }}
                        <?
                        $itembb = $itemb->item_id;
                        $item =  Item::whereid($itembb)->first();
                        $yoyo = $item->selling()->orderBy('price', 'asc')->first();
                        ?>
                        @if ($item->limited == '1')
                        @if ($item->rbp()->count() > 0)
                        <div class="col-md-4" style="margin-top: 8px;display: inline-block;padding-left: 10px;">
                        <a>
                            <div class="card-body h-100" style="padding-top: 0px;padding-bottom: 0px;padding-right: 0px;padding-left: 0px;">
                        <div class="card h-100" style="border-radius: 0px; width: 120px;">
                            <img style="object-fit:cover; width: 100%; height: 50px;" src="/public/uploads/catalog/{{$item->image}}">
                            <span class="badge badge-success limited">Limited</span>
                            <div class="card-body" style="padding-bottom: 10px;padding-right: 10px;padding-left: 10px;padding-top: 10px;">
                            <h6>{{$item->title}}</h6>
                            <img style="margin-bottom: 2px;" src="{{ asset('public/img/nau.png') }}"> {{number_format($item->rbpp)}}
                            </div>
                            <div class="card-footer" style="padding-bottom: 0px;padding-top: 10px;padding-right: 0px;padding-left: 10px;">
                                (Getting uid value for each item checked)<input type="checkbox" name="out_data[]" value="{{$itemb->uid}}"> <label>Trade?</label>
                            </div>
                     </div>
                    </div>
</a>
</div>
                        @else
                        
                        @endif
                        @else
                        
                        @endif
                        @endforeach
                    </div>

Any help is very appreciated, (Sorry if my post is messy. please tell me if I need to put more code samples.)非常感谢任何帮助,(对不起,如果我的帖子很乱。请告诉我是否需要放置更多代码示例。)

From what I understand your fear is that people will modify the UUID of the item and try to "sell" another item that do not have.据我了解,您担心人们会修改该项目的 UUID 并尝试“出售”另一个没有的项目。 If that is the case you should not work on the form itself, but on the security in the backend.如果是这种情况,您不应处理表单本身,而应处理后端的安全性。 That is:那是:

  1. receive an item UUID for sale (or an array of items)接收待售商品 UUID(或商品数组)
  2. .important! 。重要的! check that each of the items' UUIDs belong to that particular logged in user.检查每个项目的 UUID 是否属于该特定登录用户。
  3. if the all of the items belong to the user a) if they ALL belong to the user -- store them in the db as "selling" for that user.如果所有项目都属于用户 a) 如果它们都属于用户 - 将它们存储在数据库中作为该用户的“销售”。 b) if even one item does NOT belong to the user - return a validation error. b) 如果甚至一项不属于用户 - 返回验证错误。 They should not be able to store items for sale that they do not have.他们不应该能够存储他们没有的待售物品。

From what I read it seems you're skipping step 2. where you check the UUIDs you receive from the form.从我阅读的内容来看,您似乎正在跳过第 2 步。您可以在其中检查从表单收到的 UUID。

Changing the html form to be somehow "unchangeable" would be pretty much impossible so work with your limitations, not against them.将 html 表单更改为“不可更改”几乎是不可能的,因此请根据您的限制工作,而不是反对他们。 :) :)

Can you just include a hidden form input for each of the items which holds the uid?您可以为每个包含 uid 的项目包含一个隐藏的表单输入吗? eg例如

<input type="hidden" name="uid" id="uid" value="{{ $item->uid }}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM