Hello!
I have a model that look like this:
public function GetUserByname($input){
$this->instaUrl = 'https://api.instagram.com/v1/users/search?q='. $input .'&count=100&access_token=2105345345.1677ed0.8ca2b78qwdqd4fdfc4c9994hus935da95373d5ac';
$this->instafile = file_get_contents($this->instaUrl);
$this->instaArray = json_decode($this->instafile,true);
}
CONTROLLER
public function getUser(Request $request,City $city){
$input = ['search_user' => $request->get('search_user')];
$rules = ['search_user' => 'required'];
$validator = Validator::make($input, $rules);
if($validator->fails()){
return view('user')->withErrors($validator);
}else{
$city->GetUserByname($input['search_user']);
return view('user',compact('city'));
}
}
VIEW - - how can i recieve the $city variable from the controller and us it in Ajax??
<div class="container">
<h1>User on <img src="../public/img/Instagram_1.png" alt=""/></h1>
<div class="form-group">
{!!Form::open(['route'=>'user', 'method' => 'post'])!!}
</div>
@if($errors)
<ul>
@foreach ($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
@endif
<div class="form-group">
{!!Form::label('search_user','Who are you looking for?')!!}
{!!Form::text('search_user',null,['class'=>'form-control', 'placeholder'=>'Sök på användarens namn..'])!!}
</div>
<div class="form-group">
{!!Form::submit('Get Pics',['class' => 'btn btn-warning', 'id' => 'JRequest'])!!}
</div>
{!!Form::close()!!}
<br/>
<hr/>
{{-- This is working
@if(isset($city))
@foreach($city->instaArray['data'] as $c)
<div class="col-xs-6 col-md-3">
<div class="thumbnail">
<img src="{{$c['profile_picture']}}" alt="">
<p>Full name: {{$c['full_name']}}</p>
<p>Username: {{$c['username']}}</p>
</div>
</div>
@endforeach
@endif
--}}
<div id="showRequest">
</div>
I want to do the same thing as loop above but with Ajax and not reload the whole page to show results
<script>
$(document).ready(function(){
$("#JRequest").click(function(){
$.ajax({
type: "GET",
data: city, // the $city variable from the view???
dataType: 'json',
url: "http://localhost:8888/InstaSearch/public/user",
success: function(data) {
//loop through the array and show results...
});
});
});
</script>
</div>
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community