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

如何使用两个值的总和重定向我的表单并将其显示在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()我的两个字段的总和,总和是正确的。 但我无法将这笔款项转回视图。

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

表单:

 &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; 
   
 
 

控制器:

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongpan9760 2019-02-11 11:58
    已采纳

    When you use the redirect() function the variable $calculated isn't passed to the view. I think the best solution is passing it by 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. 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

    点赞 打赏 评论
  • dqu3974 2019-02-11 11:53
    $request->merge(['calculated' => $caluclated]);
    
    return redirect('/')->withInput($request->all());
    
    <input type="number" name="calculated" value="{{ old('calculated') }}" readonly>
    
    点赞 打赏 评论
  • dongqu2863 2019-02-11 12:01

    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();
        }
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题