当我在laravel中提交表单时,如何在隐藏字段中获取当前页面标题

时间:2019-05-02 06:28:46

标签: laravel email laravel-5 eloquent gmail

我有一个包含姓名,电子邮件和电话的表格。但是我想在用户提交表单时使用当前页面标题或URL,因为我想跟踪用户页面。请让我知道我该怎么做。 这是我的表单代码。

<form class="callus" method="post" action="{{url('/sendmailuser')}}">
    @csrf
    <div class="col-md-3 col-sm-6">
        <div class="single-query form-group">
            <input type="hidden" name="product_title" value="{{ request('request') }}" class="keyword-input" placeholder="Enter Your Name" required="">
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="single-query form-group">
            <input type="text" name="user_name" class="keyword-input" placeholder="Enter Your Name" required="">
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="single-query form-group">
            <input type="text" name="user_email" class="keyword-input" placeholder="Enter Your Email" required="">
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="single-query form-group">
            <input type="text" name="user_phone" class="keyword-input" placeholder="Enter Your Phone" required="Field is required">
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="single-query form-group">
        <input type="submit" value="submit now" class="btn-blue">
        </div>
    </div>
</form>

这是我的电子邮件发送代码...

public function sendyourmail(Request $r)
{
    $validator = Validator::make($r->all(), [
        'user_name' => 'required',
        'user_email' =>'required',
        'user_phone' =>'required',
        'user_desc' =>'required', 
    ]);
    if ($validator->fails()) {
        return redirect()->back()->withErrors($validator);
    }

    $data=[
        'user_email' => $r->user_email,
        'user_name' => $r->user_name,
        'user_phone' => $r->user_phone,
        'user_desc' => $r->user_desc,
    ];
    Mail::send('mail', $data, function($message) use($data) {
        $message->from($data['user_email'], 'Email Form Sumit');
        $message->to('sumit@gmail.com')->cc('sumit@sumit.com');
        $message->subject($data['user_desc']);
    });
    return redirect()->back()->with('message','Your Mail Has Been sent Successfully, We Will Get Back to You Shortly.');
}

1 个答案:

答案 0 :(得分:0)

您应在控制器内定义渲染页面的标题,并在指定应渲染的模板时将其作为视图的参数。

public function sendyourmailform()
{
    $title = 'Sample Page Title';
    return view('email_template.blade.php', compact('title'));
}

由于title变量已超出模板,因此您可以通过在标题section内两次渲染它来在模板内部访问它

@section('title')
    {{ $title }}
@endsection

在由模板扩展的布局页面内部(该模板呈现了表单),您应该像这样定义名为title的部分

<head>
    <title>
        @section('title')
        @endsection
    <title>
</head>

您可以访问模板部分内的title变量,该变量显示相关表单。

<form class="callus" method="post" action="{{url('/sendmailuser')}}">
    @csrf
    <div>
        <input type="hidden" name="page_title" value="{{ $title }}">
    </div>
    <!-- ... -->
</form>