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 ?>;
          }
      });
4
  • No ! You should only get changed php variable from server by ajax..... Commented Jul 11, 2019 at 8: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. Commented Jul 11, 2019 at 8:03
  • @Thomas i want to modify my variable but it is not reflecting in my blade file Commented Jul 11, 2019 at 8: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 Commented Jul 11, 2019 at 8:09

1 Answer 1

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).

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

Comments

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.