vendredi 27 mai 2016

How to submit a Form in Laravel?

I'm working on this simple form and trying to submit it. Basically I created an array of users in my HomeController.php which then renders its data in the home.blade.php file (That part works fine). However when I try to submit the form I get an error that says: Undefined variable: usersArray .Can anyone tell me what I'm doing wrong please? thank you so much in advance.

enter image description here

Heres my routes.php

// Home
Route::get('/', function() { return redirect('home'); });
Route::get('home', 'HomeController@home');
Route::post('home', 'HomeController@activateCustomer');

Here's my HomeController.php

 <?php
 namespace App\Http\Controllers;

use App\ABCWebServices\CDRWSRestOperations;
use App\Email\Emailer;
use App\Email\EmailTemplate;
use App\Http\Requests;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Log;
use PhpParser\Node\Expr\Cast\Array_;
use Session;

class HomeController extends Controller
{
public function __construct()
{
    $this->middleware('auth');
}

public function home()
{
    $loggedInUserId = Session::get('userId');

    $usersArray = array();
    $usersArray[0] = "Mike" . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"."Brown" . "&nbsp;&nbsp;&nbsp;&nbsp;" . "3032023245";
    $usersArray[1] = "Michael" . "&nbsp;&nbsp;&nbsp;&nbsp;" . "Smith" . "&nbsp;&nbsp;&nbsp;&nbsp;". "7204506534";
    $usersArray[2] = "Lauren" . "&nbsp;&nbsp;&nbsp;&nbsp;". "Barl". "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;". "7202314687";

    return view('home', [
        'userId' => $loggedInUserId,
        'usersArray' => $usersArray
    ]);
}

public function activateCustomer(Request $request)
{
    $adminId = Session::get('userId');
    $groupId = Session::get('groupId');
    $userIpAddress = $this->getIpAddress();
    $currentDateTime = date('Y-m-d H:i:s');
    $techAdminFirstName = $request->get('firstName');
    $techAdminLastName = $request->get('lastName');
    $techAdminEmail = $request->get('techEmail');
    $deliverToEmail = $request->get('deliverToEmail');

    $cdrCustomerId = CDRWSRestOperations::addCdrCustomer($adminId, $techAdminFirstName, $techAdminLastName, $techAdminEmail, $deliverToEmail, $groupId, $userIpAddress, $currentDateTime);

    if (isset($cdrCustomerId)) {
        // Success

        // Send email to the provisioning team to notify them that a new customer has been created
        $this->sendNotificationEmail($adminId, $techAdminFirstName, $techAdminLastName, $techAdminEmail, $groupId, $cdrCustomerId);

        return view('home', [
            'userId' => $adminId
        ]);
    } else {
        // Error - unable to insert new customer into DB via CDRWS

        // TODO return input
        return Redirect::back()->withErrors("Unable to activate your account.  Please try again later or contact customer support.");
    }

}

private function sendNotificationEmail($techAdminId, $techAdminFirstName, $techAdminLastName, $techAdminEmail, $groupId, $cdrCustomerId)
{
    require 'emailAddresses.php';

    if(empty($ucNotificationEmail)) {
        Log::warning('Notification email address is not set.  Please set it in the config file.');
    } else {
        Log::info('Sending notification email to ' . $ucNotificationEmail);
    }

    $subject = 'Notification - UC Delivery';

    $notificationTemplate = app_path() . '/Email/ServiceAndDeliveryTeamEmailTemplate.html';

    $emails = array(
        $ucNotificationEmail,
    );

    // ----------------------------------------
    // Start substitutions
    // ----------------------------------------
    $NotificationLetter = new EmailTemplate($notificationTemplate);
    $NotificationLetter->techAdminName = $techAdminFirstName . " " . $techAdminLastName;
    $NotificationLetter->techAdminEmail = $techAdminEmail;
    $NotificationLetter->techAdminId = $techAdminId;
    $NotificationLetter->groupId = $groupId;
    $NotificationLetter->activationLink = 'https://' . $_SERVER['HTTP_HOST'] . '/cdrprov/bve/cdrActivation.php?id=' . $cdrCustomerId;
    // ----------------------------------------
    // End substitutions
    // ----------------------------------------

    // Setup Emailer
    $Emailer = new Emailer($emails, $subject);
    $Emailer->setTemplate($NotificationLetter);

    // Send the email
    $Emailer->send();
}

Here's my Home.blade.php

{!! Form::open(array('id' => 'activateForm')) !!}

{!! csrf_field() !!}

<div>

<!---Error messages inside of box-->
<div style="padding-left: 135px; width:815px;">

@if (count($errors) > 0)
<div class="alert alert-danger">
    <ul class = "login-error-messages" style = "list-style-type:disc;">
        @foreach ($errors->all() as $error)
        <li>{!! $error !!}</li>
        @endforeach
    </ul>
   </div>
   @endif
  </div>

  <div class="home-rounded-border  center-content">
    <input type = "checkbox" id = "checkAll" />Select All<span style="padding-left: 310px;font-weight: bold;">Email</span><br/>
    @foreach($usersArray as $key => $value)
    <ul>
        <li>
            <input type="checkbox" id="checkboxUser" name="user-name-checkbox ">
            <input type = "email" class="styled-text  rounded" name = "name" id = "customer-name-inputField" placeholder=""/><br/><br/>
        </li>
    </ul>
    @endforeach

<center><input type = "submit" class="sign-in-button"value = "Submit"/></center>

   </br>
   <div id="statusMsg" style="margin: 0px 40px 0px 40px; background-color: #ffffff;"></div>

   <ul><li class="logout"><a href="logout"><span>Logout</span></a></li></ul>

  </div>

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire