bmm bmm - 24 days ago 8
PHP Question

How do I pass a variable into another page/view?

I would like to have the following scenario. A user selects two date periods which selects contacts from a database for the time period.

Then on a next page, I would want to display the contacts passed and also have a text area field where the user can type the message.

The problem is that by the time the next page loads

$contacts
variable is empty. So how do I pass the variables across two pages?

Below is the code for
Select Time Period


<form method="POST" action="selectContacts">
{{ csrf_field() }}
<div class="row">
<div class="col-lg-3 col-sm-3 col-xs-6">
<div class="form-group{{ $errors->has('date') ? ' has-error' : '' }}">
<label for="Event Date">Start Date</label>
<input type ='hidden' name='email' value='{{$email}}'>
<input type ='hidden' name='ip' value='{{$ip}}'>
<input name="start" type="text" class="form-control" id='fromperiod' placeholder="Date" required
@if ($errors->has('date'))
<span class="help-block">
<strong>{{ $errors->first('date') }}</strong>
</span>
@endif
</div>
</div>
</div>
<div class="col-lg-3 col-sm-3 col-xs-6">
<div class="form-group{{ $errors->has('date') ? ' has-error' : '' }}">
<label for="Event Date">End Date</label>
<input name="stop" type="text" class="form-control" id='toperiod' placeholder="Date" required
@if ($errors->has('date'))
<span class="help-block">
<strong>{{ $errors->first('date') }}</strong>
</span>
@endif
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-sm-12 col-xs-12">

<input type="submit" class="btn btn-primary pull-right" value="Select">
<a href="sendsms" class="btn btn-default pull-left">
<i class="fa fa-arrow-left" aria-hidden="true"></i> Back </a>
</div>
</div>
</form>


Then the controller that handles the
selectContacts
route.

public function SelectContacts(Request $request)
{
$Startdate=$request->start;
$Stopdate=$request->stop;
validator = Validator::make($request->all(), [
'start' => 'required|min:10',
'stop' => 'required|min:10', ]);

if ($validator->fails()) {
return redirect('/sendsms')
->withErrors($validator)
->withInput();}
$sdate=date_create("$Startdate");
$start = date_format($sdate,"Y/m/d H:i:s");

$date=date_create("$Stopdate");
$stop = date_format($date,"Y/m/d H:i:s");

$contacts = DB::table('Payment')
->whereBetween('time_paid', [$start, $stop])
->paginate(5);

//next page
return view('BulkSMS.send',
'contacts'=>$contacts,
]);
}


Then lastly the view that handles the text area nad the display of the contacts:

<form class="form-horizontal" role="form" method="post" action="/fun/sendbulk">
{{ csrf_field() }}
<div class="form-group">
<label for="message" class="col-sm-1 control-label">Message</label>
<div class="col-sm-6">
<textarea class="form-control" rows="4" name="message" required="Please Type Your Message Here" placeholder="Message"></textarea>
</div>
<input type ='hidden' name='email' value='{{$email}}'>
<input type ='hidden' name='ip' value='{{$ip}}'>
</div>
<div class="form-group">
<div class="col-sm-6 col-sm-offset-1">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary pull-right">
<a href="sendsms" class="btn btn-default pull-left">
<i class="fa fa-arrow-left" aria-hidden="true"></i> Cancel </a>
</div>
</div>
</form>
</div>

<div class="row">
<div class="col-sm-4 col-sm-offset-2">
<table class="table table-hover">
<theah>
<th>ID</th><th>Name</th> <th>Mobile Number</th> <th>Remove</th>
</theah>
<tbody>
@foreach($contacts as $contact)
<tr>
<td> {{$contact->id }} </td>
<td> {{$contact->name}}</td>
<td> {{$contact->msisdn}}</td>
<td> <form action= '' method='post'>
{{csrf_field()}}
<input type="radio" name="remove" value="other">
<!-- <input type='submit' name='submit' value='submit'> --></td></form>
</tr>
@endforeach
</tbody>
</table>
<div class="pull-right">{{ $contacts->links() }}</div></div></div>
</div>


Lastly, the controller that handles the
/fun/sendbulk
route.

public function sendsms(Request $request){
$message=$request->message;
$validator = Validator::make($request->all(), [
'message' => 'required|max:160',]);
if ($validator->fails()) {
return redirect('/selectContacts')
->withErrors($validator)
->withInput();}
$mob_numbers = implode(", " , $contacts);
$serviceArguments = array(
"mobilenumber" => $mobileNum,
"message" => $message
);
$client = new SoapClient("http://#smsws?wsdl");

$result = $client->process($serviceArguments)
}


Kindly advice

Answer

In the smssend function add this to the top.

$start=$request->start;
$stop=$request->stop;

$contacts = DB::table('Payment')
  ->whereBetween('time_paid', [$start, $stop])
  ->paginate(5); 

EDIT: i Saw you need to do some more stuff: In select contacts route:

return view('BulkSMS.send', 
    'contacts'=>$contacts,
     'start' => $start,
     'stop' => $stop
);

And in the contact view:

<input type ='hidden' name='start' value='{{$start}}'>
<input type ='hidden' name='stop' value='{{$stop}}'>
Comments