Laravel contact form confirmation

I'm new to Laravel, but I was able to create a contact form and show validation errors if any.

However, I have one problem and I 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) that says "Thank you, message has been sent."

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('info@company.com', '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?

+3


source to share


3 answers


You can use a with

class method Redirect

:

if ($validator -> passes()) {
    // data is valid
    Mail::send('emails.message', $formData, function($message) use ($formData) {
        $message -> from($formData['email'], $formData['name']);
        $message -> to('info@company.com', '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 are redirected to the contacts page using the session variables success

and message

.

Use them for warning in your view, for example. in bootstrap alert:

with a click



@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

      

no click

<?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 use them like this, you can even provide event alerts, informational alerts, or whatever alerts you want.

+1


source


When your data is INVALID, you use a method withErrors()

to transfer some data (erros) to your route. You can use the same process with any data.

For example:

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

      



This method withMessage()

will create a new variable message

and store it in the session for one request cycle.

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

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

      

0


source


I am assuming you are using Bootstrap to have this answer display a message in a popup (I am testing it for Laravel 5)

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

      

Make sure this code will be added to the 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 to helper.php so you can use it anywhere in your code

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 on the page

  {!! message_pop_up_window($message) !!}

      

0


source







All Articles