A problem echo'ing javascript in PHP (Resolved)

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

A problem echo'ing javascript in PHP (Resolved)

Post 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
Last edited by Ark on Tue Aug 30, 2011 2:41 am, edited 1 time in total.
Orgullo Catracho
User avatar
Verahta
Posts: 440
Joined: Wed Aug 24, 2011 1:50 am

Re: Please help me!

Post 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.
"In order to understand recursion, one must first understand recursion".
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Please help me!

Post 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".
The indelible lord of tl;dr
User avatar
Verahta
Posts: 440
Joined: Wed Aug 24, 2011 1:50 am

Re: Please help me!

Post by Verahta »

Okay, some of the forum sections overlap a bit so it's not always clear where to post something.
"In order to understand recursion, one must first understand recursion".
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Please help me!

Post 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.
The indelible lord of tl;dr
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: A problem echo'ing javascript in PHP

Post 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'/>\"";
Orgullo Catracho
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: A problem echo'ing javascript in PHP

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: A problem echo'ing javascript in PHP

Post 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 ;)
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: A problem echo'ing javascript in PHP

Post by Xaleph »

Ahhhhh, got it :)
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: A problem echo'ing javascript in PHP

Post by hallsofvallhalla »

marked this solved.
Post Reply

Return to “Beginner Help and Support”