Checking if 2 php variables are set inside Javascript isset





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have 2 php variables in PHP (mainly $usm and $ag) and am passing them to the frontend. In Javascript am using isset to check if they have a value before executing some code but seems not to work



<script>
if( <?php isset($usm , $ag) ?> ){
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
}
</script









share|improve this question























  • You may not access the PHP variable within the script. Try to set the php variable in cookies or sessions and extract the php variable data from there.

    – Ramya
    Jan 3 at 9:55


















0















I have 2 php variables in PHP (mainly $usm and $ag) and am passing them to the frontend. In Javascript am using isset to check if they have a value before executing some code but seems not to work



<script>
if( <?php isset($usm , $ag) ?> ){
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
}
</script









share|improve this question























  • You may not access the PHP variable within the script. Try to set the php variable in cookies or sessions and extract the php variable data from there.

    – Ramya
    Jan 3 at 9:55














0












0








0








I have 2 php variables in PHP (mainly $usm and $ag) and am passing them to the frontend. In Javascript am using isset to check if they have a value before executing some code but seems not to work



<script>
if( <?php isset($usm , $ag) ?> ){
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
}
</script









share|improve this question














I have 2 php variables in PHP (mainly $usm and $ag) and am passing them to the frontend. In Javascript am using isset to check if they have a value before executing some code but seems not to work



<script>
if( <?php isset($usm , $ag) ?> ){
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
}
</script






javascript php variables






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 9:52









MartinMartin

1841111




1841111













  • You may not access the PHP variable within the script. Try to set the php variable in cookies or sessions and extract the php variable data from there.

    – Ramya
    Jan 3 at 9:55



















  • You may not access the PHP variable within the script. Try to set the php variable in cookies or sessions and extract the php variable data from there.

    – Ramya
    Jan 3 at 9:55

















You may not access the PHP variable within the script. Try to set the php variable in cookies or sessions and extract the php variable data from there.

– Ramya
Jan 3 at 9:55





You may not access the PHP variable within the script. Try to set the php variable in cookies or sessions and extract the php variable data from there.

– Ramya
Jan 3 at 9:55












4 Answers
4






active

oldest

votes


















1














You need to check the isset part with PHP only.



<?php if(isset($usm) and isset($ag)){ ?>
<script>
$( document ).ready(function() {
var usmData = '<?php echo json_encode($usm); ?>';
var agData = '<?php echo json_encode($ag); ?>';
});
</script>
<?php } ?>





share|improve this answer


























  • JS strings need to be surrounded by ' or "

    – Vahid Amiri
    Jan 3 at 10:05











  • Yes fixed also his encoding in JSON

    – executable
    Jan 3 at 10:06











  • @executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'

    – Martin
    Jan 3 at 10:08













  • Can you show a sample data of $usm and $ag

    – executable
    Jan 3 at 10:08













  • Noted the error,, it works fine thanks alot..

    – Martin
    Jan 3 at 10:13



















2














That doesn't make sense. You mixed up PHP and JS.



Result of PHP isset is only used on PHP and doesn't make it to JS side. Your JS if statement is not valid.



Check this article for more info.






share|improve this answer


























  • But I placed it inside php tags <?php ... ?>

    – Martin
    Jan 3 at 9:58






  • 1





    @Martin It doesn't matter because Javascript expects a boolean value inside if and your the result of your PHP code doesn't get passed to JS. not like that anyway.

    – TuckerS
    Jan 3 at 10:01



















0














<script>
<?php if(isset($usm , $ag)) { ?>
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});

<?php } ?>
</script>





share|improve this answer































    0














    I would suggest you to write the PHP code outside otherwise script tag will be executed unnecessary with empty code if the condition is not met.



    <?php 
    if(isset($usm , $ag) {
    ?>
    <script>
    $( document ).ready(function() {
    var usmData = '<?php echo json_encode($usm); ?>';
    var agData = '<?php echo json_encode($ag); ?>';
    });
    </script>
    <?php
    }
    ?>





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019850%2fchecking-if-2-php-variables-are-set-inside-javascript-isset%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You need to check the isset part with PHP only.



      <?php if(isset($usm) and isset($ag)){ ?>
      <script>
      $( document ).ready(function() {
      var usmData = '<?php echo json_encode($usm); ?>';
      var agData = '<?php echo json_encode($ag); ?>';
      });
      </script>
      <?php } ?>





      share|improve this answer


























      • JS strings need to be surrounded by ' or "

        – Vahid Amiri
        Jan 3 at 10:05











      • Yes fixed also his encoding in JSON

        – executable
        Jan 3 at 10:06











      • @executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'

        – Martin
        Jan 3 at 10:08













      • Can you show a sample data of $usm and $ag

        – executable
        Jan 3 at 10:08













      • Noted the error,, it works fine thanks alot..

        – Martin
        Jan 3 at 10:13
















      1














      You need to check the isset part with PHP only.



      <?php if(isset($usm) and isset($ag)){ ?>
      <script>
      $( document ).ready(function() {
      var usmData = '<?php echo json_encode($usm); ?>';
      var agData = '<?php echo json_encode($ag); ?>';
      });
      </script>
      <?php } ?>





      share|improve this answer


























      • JS strings need to be surrounded by ' or "

        – Vahid Amiri
        Jan 3 at 10:05











      • Yes fixed also his encoding in JSON

        – executable
        Jan 3 at 10:06











      • @executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'

        – Martin
        Jan 3 at 10:08













      • Can you show a sample data of $usm and $ag

        – executable
        Jan 3 at 10:08













      • Noted the error,, it works fine thanks alot..

        – Martin
        Jan 3 at 10:13














      1












      1








      1







      You need to check the isset part with PHP only.



      <?php if(isset($usm) and isset($ag)){ ?>
      <script>
      $( document ).ready(function() {
      var usmData = '<?php echo json_encode($usm); ?>';
      var agData = '<?php echo json_encode($ag); ?>';
      });
      </script>
      <?php } ?>





      share|improve this answer















      You need to check the isset part with PHP only.



      <?php if(isset($usm) and isset($ag)){ ?>
      <script>
      $( document ).ready(function() {
      var usmData = '<?php echo json_encode($usm); ?>';
      var agData = '<?php echo json_encode($ag); ?>';
      });
      </script>
      <?php } ?>






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 3 at 10:14

























      answered Jan 3 at 9:59









      executableexecutable

      1,9832924




      1,9832924













      • JS strings need to be surrounded by ' or "

        – Vahid Amiri
        Jan 3 at 10:05











      • Yes fixed also his encoding in JSON

        – executable
        Jan 3 at 10:06











      • @executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'

        – Martin
        Jan 3 at 10:08













      • Can you show a sample data of $usm and $ag

        – executable
        Jan 3 at 10:08













      • Noted the error,, it works fine thanks alot..

        – Martin
        Jan 3 at 10:13



















      • JS strings need to be surrounded by ' or "

        – Vahid Amiri
        Jan 3 at 10:05











      • Yes fixed also his encoding in JSON

        – executable
        Jan 3 at 10:06











      • @executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'

        – Martin
        Jan 3 at 10:08













      • Can you show a sample data of $usm and $ag

        – executable
        Jan 3 at 10:08













      • Noted the error,, it works fine thanks alot..

        – Martin
        Jan 3 at 10:13

















      JS strings need to be surrounded by ' or "

      – Vahid Amiri
      Jan 3 at 10:05





      JS strings need to be surrounded by ' or "

      – Vahid Amiri
      Jan 3 at 10:05













      Yes fixed also his encoding in JSON

      – executable
      Jan 3 at 10:06





      Yes fixed also his encoding in JSON

      – executable
      Jan 3 at 10:06













      @executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'

      – Martin
      Jan 3 at 10:08







      @executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'

      – Martin
      Jan 3 at 10:08















      Can you show a sample data of $usm and $ag

      – executable
      Jan 3 at 10:08







      Can you show a sample data of $usm and $ag

      – executable
      Jan 3 at 10:08















      Noted the error,, it works fine thanks alot..

      – Martin
      Jan 3 at 10:13





      Noted the error,, it works fine thanks alot..

      – Martin
      Jan 3 at 10:13













      2














      That doesn't make sense. You mixed up PHP and JS.



      Result of PHP isset is only used on PHP and doesn't make it to JS side. Your JS if statement is not valid.



      Check this article for more info.






      share|improve this answer


























      • But I placed it inside php tags <?php ... ?>

        – Martin
        Jan 3 at 9:58






      • 1





        @Martin It doesn't matter because Javascript expects a boolean value inside if and your the result of your PHP code doesn't get passed to JS. not like that anyway.

        – TuckerS
        Jan 3 at 10:01
















      2














      That doesn't make sense. You mixed up PHP and JS.



      Result of PHP isset is only used on PHP and doesn't make it to JS side. Your JS if statement is not valid.



      Check this article for more info.






      share|improve this answer


























      • But I placed it inside php tags <?php ... ?>

        – Martin
        Jan 3 at 9:58






      • 1





        @Martin It doesn't matter because Javascript expects a boolean value inside if and your the result of your PHP code doesn't get passed to JS. not like that anyway.

        – TuckerS
        Jan 3 at 10:01














      2












      2








      2







      That doesn't make sense. You mixed up PHP and JS.



      Result of PHP isset is only used on PHP and doesn't make it to JS side. Your JS if statement is not valid.



      Check this article for more info.






      share|improve this answer















      That doesn't make sense. You mixed up PHP and JS.



      Result of PHP isset is only used on PHP and doesn't make it to JS side. Your JS if statement is not valid.



      Check this article for more info.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 3 at 11:12









      MorganFreeFarm

      1,48431328




      1,48431328










      answered Jan 3 at 9:57









      TuckerSTuckerS

      3313




      3313













      • But I placed it inside php tags <?php ... ?>

        – Martin
        Jan 3 at 9:58






      • 1





        @Martin It doesn't matter because Javascript expects a boolean value inside if and your the result of your PHP code doesn't get passed to JS. not like that anyway.

        – TuckerS
        Jan 3 at 10:01



















      • But I placed it inside php tags <?php ... ?>

        – Martin
        Jan 3 at 9:58






      • 1





        @Martin It doesn't matter because Javascript expects a boolean value inside if and your the result of your PHP code doesn't get passed to JS. not like that anyway.

        – TuckerS
        Jan 3 at 10:01

















      But I placed it inside php tags <?php ... ?>

      – Martin
      Jan 3 at 9:58





      But I placed it inside php tags <?php ... ?>

      – Martin
      Jan 3 at 9:58




      1




      1





      @Martin It doesn't matter because Javascript expects a boolean value inside if and your the result of your PHP code doesn't get passed to JS. not like that anyway.

      – TuckerS
      Jan 3 at 10:01





      @Martin It doesn't matter because Javascript expects a boolean value inside if and your the result of your PHP code doesn't get passed to JS. not like that anyway.

      – TuckerS
      Jan 3 at 10:01











      0














      <script>
      <?php if(isset($usm , $ag)) { ?>
      $( document ).ready(function() {
      var usmData = {!! json_encode($usm) !!};
      var agData = {!! json_encode($ag) !!};
      });

      <?php } ?>
      </script>





      share|improve this answer




























        0














        <script>
        <?php if(isset($usm , $ag)) { ?>
        $( document ).ready(function() {
        var usmData = {!! json_encode($usm) !!};
        var agData = {!! json_encode($ag) !!};
        });

        <?php } ?>
        </script>





        share|improve this answer


























          0












          0








          0







          <script>
          <?php if(isset($usm , $ag)) { ?>
          $( document ).ready(function() {
          var usmData = {!! json_encode($usm) !!};
          var agData = {!! json_encode($ag) !!};
          });

          <?php } ?>
          </script>





          share|improve this answer













          <script>
          <?php if(isset($usm , $ag)) { ?>
          $( document ).ready(function() {
          var usmData = {!! json_encode($usm) !!};
          var agData = {!! json_encode($ag) !!};
          });

          <?php } ?>
          </script>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 10:00









          TDKTDK

          961720




          961720























              0














              I would suggest you to write the PHP code outside otherwise script tag will be executed unnecessary with empty code if the condition is not met.



              <?php 
              if(isset($usm , $ag) {
              ?>
              <script>
              $( document ).ready(function() {
              var usmData = '<?php echo json_encode($usm); ?>';
              var agData = '<?php echo json_encode($ag); ?>';
              });
              </script>
              <?php
              }
              ?>





              share|improve this answer




























                0














                I would suggest you to write the PHP code outside otherwise script tag will be executed unnecessary with empty code if the condition is not met.



                <?php 
                if(isset($usm , $ag) {
                ?>
                <script>
                $( document ).ready(function() {
                var usmData = '<?php echo json_encode($usm); ?>';
                var agData = '<?php echo json_encode($ag); ?>';
                });
                </script>
                <?php
                }
                ?>





                share|improve this answer


























                  0












                  0








                  0







                  I would suggest you to write the PHP code outside otherwise script tag will be executed unnecessary with empty code if the condition is not met.



                  <?php 
                  if(isset($usm , $ag) {
                  ?>
                  <script>
                  $( document ).ready(function() {
                  var usmData = '<?php echo json_encode($usm); ?>';
                  var agData = '<?php echo json_encode($ag); ?>';
                  });
                  </script>
                  <?php
                  }
                  ?>





                  share|improve this answer













                  I would suggest you to write the PHP code outside otherwise script tag will be executed unnecessary with empty code if the condition is not met.



                  <?php 
                  if(isset($usm , $ag) {
                  ?>
                  <script>
                  $( document ).ready(function() {
                  var usmData = '<?php echo json_encode($usm); ?>';
                  var agData = '<?php echo json_encode($ag); ?>';
                  });
                  </script>
                  <?php
                  }
                  ?>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 10:11









                  Rajendra aroraRajendra arora

                  1,51211018




                  1,51211018






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019850%2fchecking-if-2-php-variables-are-set-inside-javascript-isset%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith