0

I have a condition in my blade file @if($url['applied'] == true) and i want to change this condition on basis of my ajax call value.. I have tried

jQuery.ajax({
          type: "",
          headers: '',
          url: '', 
          data: '',
          success: function(data) {
            <?php $url['applied'] = false ?>;
          }
      });
Jainam Shah
  • 204
  • 1
  • 9
  • No ! You should only get changed php variable from server by ajax..... – hs-dev2 MR Jul 11 '19 at 08:00
  • 1
    I think you might want to read up on how the frontend (your JavaScript) talks to the backend (your Laravel app), because it's: 1) browser asks for webpage including JavaScript 2) Laravel gives that to the browser 3) You can execute Javascript to ask for more data, but what was sent in step 1 stays the same, you can only modify it. – online Thomas Jul 11 '19 at 08:03
  • @Thomas i want to modify my variable but it is not reflecting in my blade file – Jainam Shah Jul 11 '19 at 08:04
  • @JainamShah Yes I know and I try to explain as to why it does not work. But if you just want to change that var, make it persistent by setting it in a session var. Those will persist between page loads. This will however not reload the page – online Thomas Jul 11 '19 at 08:09

1 Answers1

1

I think you have misunderstanding/misconception here.

The ajax call can be made after the PHP (Laravel) renders the HTML file and load it into your browser. Then the ajax call is done in your browser, not in the server. So you cannot asign a PHP variable from client/browser side.

What you can do is that you can create a JavaScript variable inside blade file (which is not recommended btw (https://stackoverflow.com/a/23740549/1331040)):

var urlApplied = {{$url['applied']}};

Then hide/show the content based on ajax response:

jQuery.ajax({
      ...
      success: function(data) {
        urlApplied = data.response; // or whatever the property you have instead of 'response'
      }
  });

Since you haven't provided the content you want to hide/show, I cannot give more specific hide/show logic but hope this gives you a clue.

But I strongly suggest you to reconsider the logic by decoupling the browser and client side flows (you can read the answers in the link I shared above).

Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35