0

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 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

Comments

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

your code must be in .php file

Comments

0

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

2 Comments

I did not understand your answer, can you please explain it
document.write() ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.