简体   繁体   中英

How to use the jquery variable in script inside the PHP tag echo?

I am getting the issue

Warning: Use of undefined constant otherInput - assumed 'otherInput' (this will throw an Error in a future version of PHP) Warning: A non-numeric value encountered

I know there is some issue with the quote.

What I am doing is, I have to click on the anchor tag called Click me one and sending the data-id in the script. I am getting the id value in the script but I am getting the error on $(".showme'+otherInput+'").show();

I am using WordPress and I have to use the below code in the function.php so I have to use my script inside the PHP tag.

This is the screenshot of the code在此处输入图片说明

Here is the code

<!DOCTYPE html>
<html>
   <head>
      <title></title>
      <style type="text/css">
         .showme{display: none;}
      </style>
   </head>
   <body>
      <div class="clickme" data-id="1">Click me one</div>
      <div class="showme showme1">this is example</div>
      <?php
         echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
         <script type="text/javascript">
             $(".clickme").click(function(){
             var otherInput=$(this).data("id");  
               $(".showme'+otherInput+'").show();
               });.
         </script>';
         ?>
   </body>
</html>

It looks like everything in your php block is all JQuery/Javascript.

Q: What do you even need the PHP block and the "echo" for?

Current:

  <?php
     echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
     <script type="text/javascript">
         $(".clickme").click(function(){
            var otherInput=$(this).data("id");  
            $(".showme'+otherInput+'").show();
         });.
     </script>';
     ?>

Suggested change:

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script type="text/javascript">
      $(".clickme").click(function(){
         var otherInput=$(this).data("id");  
         $(".showme"+otherInput).show();
      });.
  </script>'

Just follow the regular js standard, and if you willing to implement your script inside the php code:

Replace your code with these changes:

            <!DOCTYPE html>
        <html>
           <head>
              <title></title>
              <style type="text/css">
                 .showme{display: none;}
              </style>
           </head>
           <body>
              <div class="clickme" data-id="1">Click me one</div>
              <div class="showme showme1">this is example</div>
              <?php
         echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
         <script type="text/javascript">
             $(".clickme").click(function(){
                var otherInput=$(this).data("id");  
                $(".showme"+otherInput).show(); 
                
                //here otherInput js variable is just used as regular js variable inside the script, so no need to extra commas to call that js variable, just use it like you do in js files, and will work fine.
             });
         </script>';
         ?>

           </body>
        </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM