1

I am getting this error when running my page through the W3C validator:

= in an unquoted attribute value. Probable causes: Attributes running together or a URL query string in an unquoted attribute value.

Here is the code that is causing this error:

$prevPage = "?main=men&category1=1&pagenum=$prevp"."&lowRowNum=$prev_page_low".
                    "&highRowNum=$prev_page_high";

print("<a class='left' href=$prevPage>".
                    "Previous Page</a>&nbsp;&nbsp;&nbsp;");

I am not getting any error like this in any of my other PHP scripts , so I am guessing it is something specific I am doing wrong here?

Can anyone see why this code is causing this error?

I have researched this and read that it may be down to HTML special characters? I am not sure..

Thank you

3
  • You are trying to validate HTML. Show us HTML, not PHP. Commented Apr 30, 2012 at 16:24
  • The HTML in question was the href anchor in my post, this was the HTML that was causing the issue, I have received an answer which solved my problem. Thank you. Commented Apr 30, 2012 at 16:30
  • Why is my question being down voted? Its a perfectly valid question!! Commented Apr 30, 2012 at 16:32

2 Answers 2

6

Simply try:

<a class='left' href='$prevPage'>

Notice the quotes. Always try to quote your attributes in HTML. Better practice would be to:

echo '<a class="left" href="' . $prevPage . '">' //etc etc;

Try not to echo the entire HTML string as you might get confused when using single vs. double quotes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, that solved my problem, it would have taken me a century to notice that! I appreciate your help!
2

I would suggest using a combination of printf() and htmlentities() here:

printf(
  '<a class="left" href="%s">Previous Page</a>'
    , htmlentities($prevPage)
);

printf() takes a string and echos it after replacing specially-formatted tokens. In the above example, there is one token in the string, represented as %s.

When printf() runs, it replaces each token with a corresponding parameter. In the above example, the %s is replaced by the result of htmlentities($prevPage).

The manual page for sprintf() goes into more detail about how the tokens work and has many more examples (sprintf() and printf() are identical except that sprintf() returns the formatted string, while printf() outputs it directly).

To ensure that entities such as & are encoded correctly for markup validation, you should also make use of htmlentities().

1 Comment

That's excellent, I read about this when researching my problem, I never understood it, but I like your suggestion, thanks for explaining that for me!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.