skip to Main Content

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
enter image description here

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>

2

Answers


  1. 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>'
    
    Login or Signup to reply.
  2. 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search