Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

javascript - how to use a PHP variable inside <script> tag

I am passing PHP variables in URL, so my page link looks like this www.example.com?redirect=neworders

Based on $_GET['redirect'] value I create the content of my page in my example the input

Then in the <script> part I want to call a function with parameters based on the PHP value.

My current code is working for now But I believe that's not the correct way how it should be done although I checked multiple threads and usually answers were one of the option I mentioned.

Any suggestions please if I am doing it right ? I added comments in my code to be understand my case. Thank you very much.

    <?php 
    
   //my url can be either www.example.com?redirect=neworders or www.example.com?redirect=deliveredorders

    switch($_GET['redirect']) {
      case 'neworders':
          $page = 'Backlog'; 
          break;
      case 'deliveredorders':  
          $page = 'Shipment';   
          break;
          }
    
    ?>
    
      <body>
    //based on $page value, I create the input value with a specific id
    <?php  
    if ( $page == 'neworders') { 
     echo '<input type="text" id="BacklogName" placeholder="enter order number">'; }
    elseif ( $page == 'deliveredorders') { 
     echo '<input type="text id="ShipmentName" placeholder="enter order number then serial number">'; 
    } 
    ?> 
    
    <button type="button" id="submitButton">Submit</button>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    
      </body>
    
    <script>
    $(document).ready(function() { 
    
      //I have some functions declared here to use based on $page value

    function submitEvent(element, input) {
    element.click(function() {      
      alert(input.val());
      }); 
    }
    
    //based on $page value, intialize the function with exact selectors
    
    //first Option came to my mind : check the $page value and echo function intialization as string
    <?php 
    if ( $page == 'neworders') { 
      echo 'submitEvent($("#submitButton"), $("#BacklogName"));'; }
    elseif ( $page == 'deliveredorders') { 
      echo 'submitEvent($("#submitButton"), $("#ShipmentName"));'; } 
    ?> 
    
    //second option came to my mind : use javascript 'if else' and only echo the $page value
    
    if ("<?php echo $page; ?>" == "neworders") { 
      submitEvent($("#submitButton"), $("#BacklogName")); }
    else if ("<?php echo $page; ?>" == "deliveredorders") { 
      submitEvent($("#submitButton"), $("#ShipmentName")); 
    }
    
    });
    </script>
question from:https://stackoverflow.com/questions/65916944/how-to-use-a-php-variable-inside-script-tag

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use it like :

<script type="text/javascript">
  
    var baz = <?php echo 42; ?>;
    alert(baz);
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...