Page 1 of 1

simple echoing javascript with php solved

Posted: Sat Jan 23, 2010 2:18 pm
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>

Re: simple echoing javascript with php, escape quotes problem

Posted: Sat Jan 23, 2010 2:58 pm
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>'\"";
 

Re: simple echoing javascript with php, escape quotes problem

Posted: Sat Jan 23, 2010 3:23 pm
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 "?

Re: simple echoing javascript with php, escape quotes problem

Posted: Sat Jan 23, 2010 3:44 pm
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.

Re: simple echoing javascript with php, escape quotes problem

Posted: Sat Jan 23, 2010 4:00 pm
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

Re: simple echoing javascript with php, escape quotes problem

Posted: Sat Jan 23, 2010 4:12 pm
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.

Re: simple echoing javascript with php, escape quotes problem

Posted: Sat Jan 23, 2010 4:16 pm
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.

Re: simple echoing javascript with php, escape quotes problem

Posted: Sat Jan 23, 2010 4:19 pm
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.

Re: simple echoing javascript with php, escape quotes problem

Posted: Sat Jan 23, 2010 8:02 pm
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.