dpw70180
2017-06-24 10:11
浏览 46

如何在javascript内部HTML中编写php代码

How to write PHP code inside javascript inner HTML, I want to write PHP code inside javascript innerHTML that contain HTML code

<?php
<script>
document.getElementById("view-img-path"+id).innerHTML ="<input type='hidden' name='new-image-attachment-id' id='image-attachment-id' value='"<?php echo  get_option('media_selector_attachment_id'); ?>"'/>"
</script>

I want to write PHP code inside, is my code is correct it is showing error in my IDE

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanjiu1950 2017-06-24 10:53
    已采纳
        var option = '<?php echo  get_option('media_selector_attachment_id'); ?>';
        document.getElementById("view-img-path"+id).innerHTML ="<input type='hidden' name='new-image-attachment-id' id='image-attachment-id' value='+ option +'/>"
    

    your code must be in .php file

    点赞 评论
  • dqqn32019 2017-06-24 10:14

    I Just Create One .php File and then using php code Include to include that php file in Js.

    myfile.php

    <input type='hidden' name='new-image-attachment-id' id='image-attachment-id' value='<?php $myvalue = get_option('media_selector_attachment_id');  echo $myvalue;  ?>
    

    HTML

       <script>
          document.getElementById("view-img-path"+id).innerHTML ='<?php include_once "myfile.php";?>'
       </script>
    

    OR ( LiKe In Your Case )

    <script>
    
    document.getElementById("view-img-path"+id).innerHTML ="<input type='hidden' name='new-image-attachment-id' id='image-attachment-id' value='<?php $myvalue = get_option('media_selector_attachment_id');  echo $myvalue;  ?>'/>"
    
    </script
    

    OR

    var option = '<?php echo  get_option('media_selector_attachment_id'); ?>';
    
    document.getElementById("view-img-path"+id).innerHTML ="<input type='hidden' name='new-image-attachment-id' id='image-attachment-id' value='+ option +'/>"
    
    点赞 评论
  • dty47696 2017-06-24 10:17

    remove " outside php tag. Also remove <?php before <script>

    <script>
    document.getElementById("view-img-path"+id).innerHTML ="<input type='hidden' name='new-image-attachment-id' id='image-attachment-id' value='<?php echo  get_option('media_selector_attachment_id'); ?>'/>"
    </script>
    
    点赞 评论

相关推荐 更多相似问题