1

I want to get some code snippets from a PHP server to be "injected" in HTML. Basically when I open page.php I get some text like "_off".

The injection works with this code, however I can't use php in this html since it is local.

<img src="images/test
<?php
{ echo "_off"; }
?>
.jpg">

JavaScript seems to be the logical step. But if I just enter this at the position where I want the text of course it doesn't work:

<script type="text/javascript"> $.get( "page.php", function( data ) { document.write(data); } ); </script>

Any ideas?

1
  • 2
    you can't use document.write like that. not after the page load has completed... since you're using jquery, just use standard dom injection techniques. e.g. $('#someelement').load('page.php') Commented Feb 23, 2016 at 18:38

1 Answer 1

2

You need to use $('#someElement').html(data) or $('#someElement').text(data) if you want to write the data to a specific place on the page. You should avoid using document.write

Here is a fiddle to demonstrate: https://jsfiddle.net/yd9mx4d3/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, however like this it is not possible to add a string to a file name or is there a way to do this?
I'm not sure that I follow, but you don't have to set the text or html, you can also set a property $('#someImage').prop('src', data); or you could append to an existing property as well

Your Answer

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