Form problems using PHP with Laravel

Asked

Viewed 209 times

0

Next staff I developed a website using laravel I am programming a form but it has no action I have already finalized it does not work when I click to send, it does not do anything only from reload on the page and does not appear even the errors even if I fill or leave empty it does not work someone else can help me follow my code it will be sent to my g-accountmail but I use my own domain instead of using [email protected] the domain is [email protected] follow the codes:

PHP:

<div class="row">
                   @if (count($errors) > 0)
                        <div class="alert alert-danger">
                            <ul>
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    @if (session('message'))
                        <div class="alert alert-success">
                            {!! session('message')  !!}
                        </div>
                    @endif

                    {!! Form::open(array('action' => 'FormController@postContato', 'role' => 'form', 'class'=>'form')) !!}
                    <div class="form-group">
                        <div class="row">
                            <div class="col-md-6">
                                {!! Form::text('nome', null, array('placeholder'=>'Nome')) !!}
                                {!! Form::text('email', null, array('placeholder'=>'E-mail')) !!}
                                {!! Form::text('telefone', null, array('placeholder'=>'Telefone')) !!}
                            </div>
                            <div class="col-md-6">
                                {!! Form::textarea('mensagem', null, array('placeholder'=>'Mensagem', 'rows'=>'5')) !!}
                                {!! Form::submit('Enviar', array('class' => 'btn btn-success'), array('id' => 'btn_submit')) !!}
                            </div>

                        </div>
                    </div>
                    {!! Form::close() !!}
            </div>  

Route:

Route::post('/', 'FormController@postContato');

Controller:

application/x-httpd-php FormController.php ( PHP script text )

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use Mail;
use App\Http\Requests;

class FormController extends Controller
{
    public function index() {
        return view('/');
    }


    public function postContato(Request $request) {
        $rules = array( 'nome' => 'required', 'email' => 'required|email', 'telefone' => 'required', 'mensagem' => 'required' );
        $errors = [
            'required'    => 'O campo :attribute é obrigatório.',
            'email'    => 'Digite um email válido.',
        ];
        $validation = Validator::make($request->all(), $rules, $errors);
        $data = array();
        $data['nome'] = $request->input("nome");
        $data['email'] = $request->input("email");
        $data['telefone'] = $request->input("telefone");
        $data['mensagem'] = $request->input("mensagem");

        if($validation->passes()) {
            Mail::send('emails.contato', $data, function($message) use($request){
                $message->from($request->input("email"), $request->input("nome"));
                $message->to('[email protected]') ->subject('contato do site');
                $message->bcc('[email protected]') ->subject('Contato do site');

            });
            return redirect('#contato')->with('message', 'Mensagem enviada com sucesso!');
        }
        return redirect('#contato')->withErrors($validation);
    }
}

Mail.php

<?php

return [
    'driver' => env('MAIL_DRIVER', 'smtp'),
    'host' => env('MAIL_HOST', 'smtp.agenciafront.com.br'),
    'port' => env('MAIL_PORT', 587),
    'from' => ['address' => null, 'name' => null],
    'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
    'username' => env('[email protected]'), 
    'password' => env('xxxxxx123'),
    'sendmail' => '/usr/sbin/sendmail -bs',

];

.ENV:

APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:CBnXZVj0+0xnFYhUnrOWQRKvmobN0qmB8fIFq/3pnFs=
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.agenciafront.com.br
MAIL_PORT=587
[email protected] 
MAIL_PASSWORD=xxxxx-123
MAIL_ENCRYPTION=ssl

NOTE: Access the site in the link placed in the question there you can see that the form has no action. version of Laravel 5.2

  • Tried a dd detro of the f($validation->passes()) { to see if he’s getting this far?

1 answer

1

You are sending the action directly to the controller. You must use the ROUTE NAME in the FORM action. and modify database data (DB_DATABASE=Homestead, DB_USERNAME=Homestead, DB_PASSWORD=secret) for your data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.