I am trying to send variable with data from a function to laravel front page view and i get Undefined variable: data (View: C:\xampp\htdocs\laravelproject\resources\views\process-url.blade.php).
This is my code and web.php routes:
web.php:
Route::get('/', [welcomechecker::class, 'getfrontpage']);
Route::post('process-url', [welcomechecker::class, 'saveformdata']);
welcomechecker.php controller:
class welcomechecker extends Controller {
function getfrontpage(){
return view('welcome');
}
function saveformdata(Request $request){
$client = new Client();
$data = [];
$url = $request->url; //getting value from ajax $url = $request->url;
$wp = $url.'/'.'wp-admin';
$website = $client->request('GET', $url);
$html = $website->html();
//Check if cms is wordpress
$cms = '';
if($this->isWordPress($wp, $html) == 'WordPress'){
$cms = 'WordPress';
$data['cms'] = 'WordPress';
}
return view('process-url', compact('data'));
}
}
view blade process-url.blade.php:
@foreach($data as $student)
{{$student->cms}}
@endforeach
Front page view blade welcome.blade.php:
<div class="display-content alert-success">
@include('process-url')
</div>
application.js having AJAX code:
jQuery(document).ready(function(){
$(document).ajaxStart(function(){
$("#wait").css("display", "block");
});
$(document).ajaxComplete(function(){
$("#wait").css("display", "none");
});
jQuery('#ajaxsubmit').click(function(e){
e.preventDefault();
jQuery('.alert').show();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
jQuery.ajax({
url: 'process-url',
type: 'POST',
data: {
url: jQuery('#enter_url').val()
},
success: function(result){
console.log(result);
jQuery('.display-content').html(result.success).fadeIn(700);
// jQuery('.display- content').html(result.success).fadeIn(700).fadeOut(5000);
}});
});
});
Someone please help me to identify what I am doing wrong. I am trying to submit the variable data to the process-url blade view which is routed to the saveformdata() on the web.php. The error occurs in process-url.blade.php at the data variable. The saveformdata function has a whole lot more code than what I actually put here, the whole idea is that it returns an array of data from a scraping tool which I want to display on a table on process-url blade.
return view('welcome');does not contain any data?saveFormDatamethod? If so, you're trying to access each$datavalue as an object ($student->cms) while it is a string, as it is you can just refactor$student->cmsto$studentand it will work. If this isn't the main problem, try to add more info on the question, mainly by adding the complete error (message & line) and explaining all the logic you have on the method that's failing.return view('process-url', compact('data'));in heredatais an array and in the blade you try and access it like it was an object