simple echoing javascript with php solved

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

simple echoing javascript with php solved

Post by scarface222 »

Hey guys, I have a quick issue, that is driving me crazy but would be simple for someone more experienced.

I am echoing the following javascript line in php

Code: Select all

document.getElementById('message$toalert').innerHTML=\"<div class=\'ui-widget\'>
            <div class=\'ui-state-error ui-corner-all\' style=\'padding: 0 .7em;\'> 
                <p><span class=\'ui-icon ui-icon-alert\' style=\'float: left; margin-right: .3em;\'></span> 
                <strong>Alert:</strong> \"+response+\"</div></div>\";
I have tried double quotes and escaping twice /"/" because I thought javascript needed to escape div quotes as well and have tried concatinating in certain ways and I cannot figure out what to do.

This works perfectly

Code: Select all

document.getElementById('message$toalert').innerHTML=\"+response+\";
but whenever I try to add a div with a style parameter it goes to <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>
Last edited by scarface222 on Sat Jan 23, 2010 8:33 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: simple echoing javascript with php, escape quotes problem

Post by califdon »

I haven't tested this, but I think this is probably what you need:

Code: Select all

echo "document.getElementById('message$toalert').innerHTML=\"<div class='ui-widget'>
     <div class='ui-state-error ui-corner-all' style='padding: 0 .7em;'> 
     <p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span> 
     <strong>Alert:</strong> ' + response + '</div></div>'\"";
 
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: simple echoing javascript with php, escape quotes problem

Post by scarface222 »

I tried this

Code: Select all

 
document.getElementById('message$toalert').innerHTML=\"<div class='ui-widget'>
    <div class='ui-state-error ui-corner-all' style='padding: 0 .7em;'>
    <p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span>
    <strong>Alert:</strong> ' + response + '</div></div>'\";
I appreciate the response however, it did not work. The reason I removed the echo and " on each end is because it is within a larger echo statement. The reason I left ; in is because that is ending the javascript command which you forgot to add. Are you sure that class='something' will work? you do not need to somehow escape and incorporate double quotes "?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: simple echoing javascript with php, escape quotes problem

Post by califdon »

When you are debugging something that isn't working, it's best not to "simplify" it, either when seeking help, or even for your own analysis, because you just MAY be obscuring the heart of the problem.

The way to analyze messy quoted string issues like this is to think like the parser 'thinks'. Here you're trying to create a string for PHP to send to the browser, that contains further strings that will be interpreted by Javascript. So start with the 'echo' and proceed through the entire string. If you begin the string with a double-quote, the parser will treat the next double-quote it sees as the end of the string, if it's not escaped, so if you want to use a double-quote to be passed to the Javascript string, it must be escaped. There is no reason to escape single-quotes in such a scenario. You're right about the ending semicolon for the Javascript command, that slipped my attention. Have you looked at the string this produces in the HTML source? That's what counts. You should be able to see there what else may be wrong, and work backward to make it come out right.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: simple echoing javascript with php, escape quotes problem

Post by scarface222 »

That's a good point. I checked the source and got this

Code: Select all

document.getElementById('test').innerHTML="<div class='ui-widget'>
    <div class='ui-state-error ui-corner-all' style='padding: 0 .7em;'>
    <p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span>
    <strong>Alert:</strong> testing</div></div>'";
The code does not execute at all leaving the test div blank. The problem is, this seems generally what I wanted to echo. I am pretty positive it is possible to project div content using innerHTML
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: simple echoing javascript with php, escape quotes problem

Post by scarface222 »

Code: Select all

<script>document.getElementById('test').innerHTML="<div class=\"ui-widget\">
    <div class=\"ui-state-error ui-corner-all\" style=\"padding: 0 .7em;\">
    <p><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: .3em;\"></span>
    <strong>Alert:</strong> testing</div></div>";</script>
tried that in plain html, and it doesn't even work. I think javascript may be having a problem. The thing that gets me is that I have seen illustrated examples using innerHTML to post div content and spans.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: simple echoing javascript with php, escape quotes problem

Post by califdon »

scarface222 wrote:The code does not execute at all leaving the test div blank. The problem is, this seems generally what I wanted to echo. I am pretty positive it is possible to project div content using innerHTML
I don't see anything wrong there. So now it's not a PHP problem, it's a Javascript problem. You might want to post the Javascript in our Javascript forum. No need to involve PHP at all, just show your entire JS function and the associated HTML. I'm sure someone will give you a hand with it.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: simple echoing javascript with php, escape quotes problem

Post by scarface222 »

I appreciate your time califdon, it was the weirdest problem although your insight into going to test through html led me to it. For some reason....I had to delete all the spaces between each tag and subsequent content and then it worked. Do you have any insight into why this would be? I did not think javascript was sensitive to spacing.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: simple echoing javascript with php, escape quotes problem

Post by califdon »

scarface222 wrote:I appreciate your time califdon, it was the weirdest problem although your insight into going to test through html led me to it. For some reason....I had to delete all the spaces between each tag and subsequent content and then it worked. Do you have any insight into why this would be? I did not think javascript was sensitive to spacing.
Depends on where the spacing is. Generally, spaces are required anyplace where the parser would be confused by not knowing where a command or a tag or something ends. Glad you found the problem.
Post Reply