Jump to content

W3C compliance error during HTML Validation

Recommended Posts

Hi all !

I am passing JSON ENCODED data as the img src data and I get the following error when i validate my page for HTML

Bad value “/image/image_draw.php?caption=Wrn&gd1={"2018-10-10":50,"2018-10-11":40,"2018-10-12":40,"2018-10-13":0,"2018-10-14":30}&gd2=&gd3=&gd4=” for attribute “src” on element “img”: Illegal character in query: “{” is not allowed.

How can I get rid of this error? I am passing data as JSON_ENCODED.

Thanks all !

Link to post
Share on other sites

Hi requinix,

Thanks for the response.

The data is pulled out of a database and json_encoded.

<?php echo "<img src='/image/image_draw.php?caption=Wrn&gd1=$G_wrn_w_data&gd2=$G_mtl_w_data&gd3=$G_mol_w_data&gd4=$G_dov_w_data' alt='graphs'/>"; ?>

which gets translated into HTML as

<img src='/image/image_draw.php?caption=Wrn&gd1={"2018-10-10":50,"2018-10-11":40,"2018-10-12":40,"2018-10-13":0,"2018-10-14":30}&gd2=&gd3=&gd4=' alt='graphs'/>

The problem is due to the curly braces which HTML does not validate. My graph however displays correctly. If I ignore the error of the validator, i am good but i don't wish to do that.

To generate the output I am using the phpGraphlib library by Elliott Brueggeman. I believe it may be obsolete but my graphs draw just fine !

after decoding the encoded json data

// $graph->setupYAxis(500, 'black');
// $graph->SetupXAxis(1200,'black');

I hope that answers your question.


Link to post
Share on other sites

When building query strings with anything more complicated than simple alphanumeric values, use http_build_query.

Then, because you'll be putting this into HTML, you must also use htmlspecialchars(). You're using single quotes for the attribute value so you'll need the ENT_QUOTES flag.

<?php echo "<img src='/image/image_draw.php?", htmlspecialchars(http_build_query([
  "caption" => "Wrn",
  "gd1" => $G_wrn_w_data,
  "gd2" => $G_mtl_w_data,
  "gd3" => $G_mol_w_data,
  "gd4" => $G_dov_w_data
]), ENT_QUOTES), "' alt='graphs'/>"; ?>


  • Great Answer 1
Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.