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

时间:2017-06-24 10:11:25

标签: javascript php jquery html

如何在javascript内部HTML中编写PHP代码,我想在包含HTML代码的javascript innerHTML中编写PHP代码

<?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>

我想在里面编写PHP代码,我的代码是正确的,它在我的IDE中显示错误

3 个答案:

答案 0 :(得分:1)

删除php标记之外的"。同时在<?php

之前删除<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>

答案 1 :(得分:0)

  

我只创建一个.php文件,然后使用php代码Include将该php文件包含在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>
  

(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 +'/>"

答案 2 :(得分:0)

    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 +'/>"

您的代码必须位于。 php 文件