up vote 1 down vote favorite

Hi,

I have this print statement:

print "<a href='#' onClick='document.getElementById(\"myheader\").innerHTML=\"\"'".$rowQuery['keyword']."&nbsp; ·&nbsp;</a>";

Unfortunately, it prints:

<a ·&nbsp;="" keyword&nbsp;="" onclick="document.getElementById("myheader").innerHTML=""" href="#"/>

I have no idea why, any help would be useful. Because of the way it is printed, I can't see anything on the screen and the functionality does not work either.

Note ($rowQuery['keyword'] = "keyword" in this case so it is being evaluated, that is not the problem. The problem is that it prints it weird)

(When I use HTML instead of php to print it using this line:

<a href="#" onClick="document.getElementById('myheader').innerHTML=''">ALL</a>

it works completely fine)

flag

Prints weird? As in, the attributes print out-of-order? – Ignacio Vazquez-Abrams Jun 20 at 7:11
Yes, as you can see, it prints weirdly in different order which means it doesn't function at all (i mean that I can't see what should be printed on the screen and of course the javascript line does not work either – user220755 Jun 20 at 7:13
I doubt it actually prints like that. I bet the browser you're debugging it in shows you a DOM representation, not the actual source. – deceze Jun 20 at 7:14
I am using firebug on firefox. However, the problem is, I can't see it on the screen, if I print it normally (I mean HTML and not print it through PHP it works completely fine) – user220755 Jun 20 at 7:15
1  
There is no closing tag in your first print statement. try this: print "<a href='#' onClick='document.getElementById(\"myheader\").innerHTML=\"\"'".$rowQuery['keyword'].">ALL</a>"; – Russell Dias Jun 20 at 7:18
show 2 more comments

3 Answers

up vote 0 down vote accepted

I think you are missing the closure of the <a... tag

You have:<a href=... some description</a>

Instead of <a href=... >some description</a>

Try something like this (not sure I got the location of the '>' correct:

print "<a href='#' onClick='document.getElementById(\"myheader\").innerHTML=\"\"'".$rowQuery['keyword']">."&nbsp; ·&nbsp;</a>";

link|flag
up vote 3 down vote

You're missing a closing > for the opening <a> element, and maybe there are other syntax issues I'm not spotting right now. The browser is interpreting it as best he can, which ends up being weird.

To minimize problems like this (and avoid lots of quote escaping), leave the static text static and only echo the dynamic content like this:

<a href="#"><?php echo $rowQuery['keyword']; ?></a>
link|flag
up vote 2 down vote

You will get a more accurate picture of what's getting written to PHP's output stream by viewing source instead of FireBug.

Your code doesn't appear to be closing the <a> tag. It also looks like there's an extended character · between your non-breaking spaces. You should make sure that it is supported in your character set or replace it with a HTML entity, such as &#8226;. If you must use extended characters, it's generally advisable to make sure that your PHP script, your HTML meta tags and your PHP server encoding config setting are all referring to the same character set or one stray extended character can kill your page when getting rendered in the browser. If you stick to UTF-8 for everything, you are likely to have the fewest problems (though not always).

link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.