简体   繁体   English

如何使用两个值的和来重定向表单并将其显示在Laravel中的只读输入字段中?

[英]How can I redirect my form with the sum of two values and show it in a readonly input field in Laravel?

I am making a simple form two sum two values. 我正在做一个简单的形式,两个值相加两个值。 I want to display the calculated result in an readonly input after the request went through. 请求通过后,我想在只读输入中显示计算结果。 If I dd() the sum of my two fields, the sum is correct. 如果我dd()两个字段的和,则该和是正确的。 But I am unable to pass the sum back to the view. 但是我无法将总和传递回视图。

I searched around the web and found nothing promising. 我在网上搜索,发现没有什么希望。 Same with the Laravel documentation. 与Laravel文档相同。

Form: 形成:

<p>
<input type="number" name="numberOne" value="{{ old('numberOne') }}">
</p>
<p>
<input type="number" name="numberTwo" value="{{ old('numberTwo')}}">
</p>
<p>
<input type="number" name="calculated" value="{{ isset($calculated) ? $calculated : '' }}" readonly>
</p>

Controller: 控制器:

class CalculationsController extends Controller
{
    public function process(Request $request) {
        $numberOne = $request->input('numberOne');
        $numberTwo = $request->input('numberTwo');

        $calculated = $numberOne + $numberTwo;

        dd($calculated);

        return redirect('/')->withInput();
    }
}

Expected would be the sum of both inputs in the readonly. 预期将是只读中两个输入的总和。 But after the submit, it's still empty. 但是提交后,它仍然是空的。 What am I doing wrong? 我究竟做错了什么?

$request->merge(['calculated' => $caluclated]);

return redirect('/')->withInput($request->all());

<input type="number" name="calculated" value="{{ old('calculated') }}" readonly>

When you use the redirect() function the variable $calculated isn't passed to the view. 当您使用redirect()函数时,变量$calculated不会传递给视图。 I think the best solution is passing it by Session. 我认为最好的解决方案是通过Session。 Your redirect must be something like that: 您的重定向必须是这样的:

return redirect('/')->withInput()->with('calculated', $calculated);

The with() function is used to pass data through the session. with()函数用于通过会话传递数据。 To retrieve it in your view, do: 要在您的视图中检索它,请执行以下操作:

<input type="number" name="calculated" value="{{ session()->has('calculated') ? session('calculated') : '' }}" readonly>

For documentation, check: https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data https://laravel.com/docs/5.7/session#retrieving-data 有关文档,请检查: https : //laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data https://laravel.com/docs/5.7/session#retrieving-data

Add calculated value before redirect, 在重定向前添加计算值,

class CalculationsController extends Controller
{
    public function process(Request $request) {
        $numberOne = $request->input('numberOne');
        $numberTwo = $request->input('numberTwo');

        $calculated = $numberOne + $numberTwo;
        $request->merge(['calculated' => $calculated ]);

        // dd($calculated);

        return redirect('/')->withInput();
    }
}

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

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