mercredi 22 juin 2022

how to fix Undefined variable: data on laravel view

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)
    
@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 of 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



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/z1mqfCW
via IFTTT

Aucun commentaire:

Enregistrer un commentaire