带有 Laravel 联系表的确认警告框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26674585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:21:21  来源:igfitidea点击:

Confirmation alert box with Laravel contact form

phplaravelblade

提问by J86

I am new to Laravel but have managed to get a contact form working and showing validation errors when there are some.

我是 Laravel 的新手,但已设法使联系表单正常工作并在出现验证错误时显示验证错误。

However I do have one problem and have no idea how to handle it in Laravel. When a message is sent (validation rules pass) I would like to display an alert box (Bootstrap style) saying 'Thanks, message has been sent'.

但是我确实有一个问题,不知道如何在 Laravel 中处理它。当发送消息(验证规则通过)时,我想显示一个警告框(引导程序样式),上面写着“谢谢,消息已发送”。

CODE

代码

public function postContact()
{
    $formData = Input::all();

    // input validator with its rules
    $validator = Validator::make(
        array(
            'name' => $formData['name'],
            'email' => $formData['email'],
            'subject' => $formData['subject'],
            'message' => $formData['message']
        ),
        array(
            'name' => 'required|min:3',
            'email' => 'required|email',
            'subject' => 'required|min:6',
            'message' => 'required|min:5'
        )
    );

    if ($validator -> passes()) {
        // data is valid
        Mail::send('emails.message', $formData, function($message) use ($formData) {
            $message -> from($formData['email'], $formData['name']);
            $message -> to('[email protected]', 'John Doe') -> subject($formData['subject']);
        });

        return View::make('contact');
    } else {
        // data is invalid
        return Redirect::to('/contact') -> withErrors($validator);
    }
}

How can I achieve this in Laravel 4?

我怎样才能在 Laravel 4 中实现这一点?

采纳答案by amahrt

You could use the withmethod of the Redirectclass:

您可以使用该类的with方法Redirect

if ($validator -> passes()) {
    // data is valid
    Mail::send('emails.message', $formData, function($message) use ($formData) {
        $message -> from($formData['email'], $formData['name']);
        $message -> to('[email protected]', 'John Doe') -> subject($formData['subject']);
    });

    //Redirect to contact page
    return Redirect::to('/contact')->with('success', true)->with('message','That was great!');
} else {
    // data is invalid
    return Redirect::to('/contact') -> withErrors($validator);
}

You will be redirected to the contact page with the session variables successand messageset.

您将被重定向到带有会话变量successmessage设置的联系页面。

Use them for an alert in your view, e.g. in a Bootstrap Alert:

将它们用于您视图中的警报,例如在 Bootstrap 警报中:

with Blade

带刀片

@if(Session::has('success'))
    <div class="alert alert-success">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    <strong>Success!</strong> {{ Session::get('message', '') }}
    </div>
@endif

without Blade

不带刀片

<?php if(Session::has('success')): ?>
    <div class="alert alert-success">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Success!</strong> <?php echo Session::get('message', ''); ?>
    </div>
<?php endif; ?>

If you are using them like this you can even provide success alerts, info alerts, or any alert you want to.

如果您像这样使用它们,您甚至可以提供成功警报、信息警报或任何您想要的警报。

回答by Ahmed Al Bermawy

I assume you are using Bootstrap so this answer will show the message in pop up window (I test it on Laravel 5)

我假设您正在使用 Bootstrap,因此此答案将在弹出窗口中显示消息(我在 Laravel 5 上对其进行了测试)

return View::make('contact')->with('message', "Thanks, message has been sent");

Make sure this code will be added in footer

确保将在页脚中添加此代码

<!-- Show Pop up Window if there is message called back -->
<?php
if(session('message'))
{
    echo '<script>
        document.getElementById("popup_message").click();
    </script>';
}
?>

Add this function in helper.phpso you can use it anywhere in your code

helper.php 中添加此函数,以便您可以在代码中的任何位置使用它

function message_pop_up_window($message)
{
  $display = '
  <a class="popup_message" id="popup_message" data-toggle="modal" data-target="#message" href="#"></a>
  <div class="modal fade" id="message" role="dialog">
        <div class="modal-dialog modal-md">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title">Messsage </h4>
            </div>
            <div class="modal-body">
              <p>'.$message.'</p>
            </div>
          </div>
        </div>
      </div>
    </div>';
  return $display;
}

Then call the function in you page

然后调用页面中的函数

  {!! message_pop_up_window($message) !!}

回答by nteath

When your data is INVALID you use the withErrors()method to pass some data (erros) to your route. You can use the same process with any kind of data.

当您的数据无效时,您可以使用该withErrors()方法将一些数据(错误)传递给您的路线。您可以对任何类型的数据使用相同的过程。

For example:

例如:

return View::make('contact')->withMessage("Thanks, message has been sent");

This method withMessage()will create a new variable messageand store it in the Session for one request cycle.

此方法withMessage()将创建一个新变量message并将其存储在 Session 中,以供一个请求周期使用。

So, in your view you can access it like this:

因此,在您看来,您可以像这样访问它:

@if(Session::has('message'))
<div class="alert-box success">
    {{ Session::get('message') }}
</div>
@endif