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
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
I Just Create One
.php
File and then using php codeInclude
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 +'/>"
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>