1

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.

7
  • 1
    Good code indentation would help us read the code and more importantly it will help you debug your code Take a quick look at a coding standard for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. Commented Jun 22, 2022 at 14:09
  • 1
    return view('welcome'); does not contain any data? Commented Jun 22, 2022 at 14:13
  • 1
    That's the complete code for the saveFormData method? If so, you're trying to access each $data value as an object ($student->cms) while it is a string, as it is you can just refactor $student->cms to $student and 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. Commented Jun 22, 2022 at 14:13
  • return view('process-url', compact('data')); in here data is an array and in the blade you try and access it like it was an object Commented Jun 22, 2022 at 14:14
  • 1
    You could read the linked doc Commented Jun 22, 2022 at 14:42

2 Answers 2

1

Of course you will get Undefined Variable error when you are trying to include a blade file that is waiting for an array $data which is only passed to the view when you hit the route process-url. Also it is really, really bad practice to return a view after a POST request. Anyways, to solve your error (because that's what you actually want) you can do the following:

  1. Pass the $data to the welcome page view and remove it from the process-url view
    function getfrontpage(){
        return view('welcome', [
           'data' => ['Balloon Fight', 'Donkey Kong', 'Excitebike']
        ]); 
    }

    function saveformdata(){
        return view('process-url');
    }
  1. Pass the $data array from the welcome view to the process-url view through @include
<div class="display-content alert-success" >
    @include('process-url', ['data' => $data])
</div>

Your error now disappeared. Your code still makes no sense, but this is what you wanted.

Sign up to request clarification or add additional context in comments.

10 Comments

Hi, i want to rather send from process-url to welcome view. 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
The only way you access a GET route and in the same time send a POST request to another endpoint and receive its response is through AJAX request.
Actually i am doing this through ajax
Then your question is not relevant. You are showing totally different code. How are we supposed to help you?
No its not irrelevant. The basics is same. I have actually tried to send variable from m getfrontpage function and it works well. Just that, that is not what i want. it should be from saveformdata function
|
0

In the first URL you show a welcome blade file and it includes process-url blade without data variable

you should pass data variable in getfrontpage function like saveformdata and the include directive pass variable to child blade

function getfrontpage(){
        $data = ['Balloon Fight', 'Donkey Kong', 'Excitebike'];
 
        return view('welcome', compact('data'));
    }

and data variable array type that student will show every element in the array

@foreach($data as $student)
    {{$student}}
@endforeach

3 Comments

please can you put some code for me to see.
The reason i am using saveformdata and not getfrontpage is because saveformdata actually gets data from a form scrape the data and puts the data to an array variable. saveformdata is using a post method in web.php. So now i need to send the data to process-url view and that is where i get undefined variable
But you include process-url in welcome blade file you can use @include('process-url', ['data' => [] ])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.