Reputation: 91
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
Upvotes: 0
Views: 1730
Reputation: 5092
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 +'/>"
Upvotes: 0
Reputation: 51
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
Upvotes: 0
Reputation: 16436
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>
Upvotes: 1