Page 1 of 1

A problem echo'ing javascript in PHP (Resolved)

Posted: Tue Aug 30, 2011 12:21 am
by Ark
Hello beutiful people could someone please tell me how to write this script of javascript in php correctly?

<?php
echo'document.getElementById("div1").innerHTML="<img src="images/mycity.png"/>";';
?>


That way doesn't work... :x

Re: Please help me!

Posted: Tue Aug 30, 2011 1:02 am
by Verahta
Two suggestions, 1) rename the thread title so it's descriptive or gives someone an idea of what you need help with, and B) post this in the 'Coding' section cause people looking to help others with coding will see it there more likely.

Hope you figure it out.

Re: Please help me!

Posted: Tue Aug 30, 2011 1:07 am
by Jackolantern
You have so many quotes inside quotes, you are going to have to escape them to get this to work right. To escape quotes, you do something like this when you need double-quotes inside of double-quoted strings:

Code: Select all

$myString = "Henry said, \"I can do that!\"";
When this is echo'd, it will come out as: Henry said, "I can do that!"

That is what you need to do for this line of code. Escaped characters will not affect the PHP in the way they normally do (which would close the string), and will be echo'd out as a literal, meaning they will be printed out verbatim. I won't actually escape it for you, because it will be good practice with this new technique, but I will say a definite problem exists in the image tag in your Javascript statement in regards to quotes :)

And this does not need to be in coding. It is in the right place. But yes, please do rename the thread title. Titles like "I can't solve this", or "Please help me" do nothing to help create a library of searchable solutions on this forum. A proper title would be something like "Problem with echo'ing Javascript in PHP".

Re: Please help me!

Posted: Tue Aug 30, 2011 1:14 am
by Verahta
Okay, some of the forum sections overlap a bit so it's not always clear where to post something.

Re: Please help me!

Posted: Tue Aug 30, 2011 2:25 am
by Jackolantern
They do a bit. "Coding" was supposed to kind of be a misc. coding section for C#, Python, etc. PHP was added to the forum section description, which does make it seem to overlap a bit. But I have always operated under the assumption that Beginner and Advanced support sections are primarily aimed at user's own, unique PBBGs, while the Videos' question section was aimed at people trying to emulate the tutorial PBBG. There are definitely posts that could fit in several different sections, though.

Re: A problem echo'ing javascript in PHP

Posted: Tue Aug 30, 2011 2:43 am
by Ark
Thanks Verahta for the suggestions!

Thanks alot jack!! That made my script to work out :)

So if anyone else have the same problem the solution will be:
echo"document.getElementById(\"div1\").innerHTML=\"<img src='images/mycity.png'/>\"";

Re: A problem echo'ing javascript in PHP

Posted: Tue Aug 30, 2011 2:52 am
by Xaleph
hmm regardless of escaping characters, what was the solution? Because both cases ( using single quotes or double ) should work. The only difference I saw was the semi colon. On the end.

Re: A problem echo'ing javascript in PHP

Posted: Tue Aug 30, 2011 3:01 am
by Jackolantern
Look at the image tag in the original post, Xaleph. He wasn't causing an error in PHP. He was causing an error in Javascript by having a double-quoted string for the innerHTML value, and then double-quoting the src attribute of the img tag in that same string ;)

Re: A problem echo'ing javascript in PHP

Posted: Tue Aug 30, 2011 9:36 am
by Xaleph
Ahhhhh, got it :)

Re: A problem echo'ing javascript in PHP

Posted: Tue Aug 30, 2011 11:54 am
by hallsofvallhalla
marked this solved.