dsfw2154
2019-02-11 11:40
浏览 85
已采纳

如何使用两个值的总和重定向我的表单并将其显示在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. 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.

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?

图片转代码服务由CSDN问答提供 功能建议

我正在制作一个两个和两个值的简单形式。 我想在请求通过后在readonly输入中显示计算结果。 如果我dd()我的两个字段的总和,总和是正确的。 但我无法将这笔款项转回视图。</ p>

我在网上搜索,没有发现任何有希望的事情。 与Laravel文档相同。</ p>

表单:</ p>

 &lt; p&gt; 
&lt; input type =“number”name =“numberOne  “value =”{{old('numberOne')}}“&gt; 
&lt; / p&gt; 
&lt; p&gt; 
&lt; input type =”number“name =”numberTwo“value =”{{old('  numberTwo')}}“&gt; 
&lt; / p&gt; 
&lt; p&gt; 
&lt; input type =”number“name =”calculated“value =”{{isset($ calculated)?$ calculated:''}  }“readonly&gt; 
&lt; / p&gt; 
 </ code> </ pre> 
 
 

控制器:</ p>

 类CalculationsController extends Controller 
 {  
公共功能流程(请求$请求){
 $ numberOne = $ request-&gt; input('numberOne'); 
 $ numberTwo = $ request-&gt; input('numberTwo'); 
 
 $  calculated = $ numberOne + $ numberTwo; 
 
 dd($ calculated); 
 
返回redirect('/') - &gt; withInput(); 
} 
} 
 </ code> </  pre> 
 
 

预期将是readonly中两个输入的总和。 但提交后,它仍然是空的。 我做错了什么?</ p> </ div>

3条回答 默认 最新

相关推荐 更多相似问题