Jump to content
michaelk

js encoding help utf-8

Recommended Posts

Hi,

I have the following code:

                  <input name="data" type="text" id="data" value="<?php print utf8_decode($data_attiva)?>" size="30" readonly
          onChange="aggiorna_data_nascosta()" style="font-size:15px; font-weight:bold">
                <input type="button" id="trigger" onClick="" value="scegli data" style="background-image:url(img/icons/calendar-gray.png); background-repeat:no-repeat; background-position: 5px center; padding-left:25px">
                <script type="text/javascript">
                Calendar.setup({
                    inputField     :    "data",     // id of the input field
                    ifFormat       :    "%A %d %B %Y", // format of the input field (even if hidden, this format will be honored)
                                        daFormat       :    "%A %d %B %Y",
                    button           :    "trigger",       // ID of the span where the date is to be shown
                    weekNumbers    :    "true",
                    range           :    [2002, (new Date().getFullYear())+2]
                });
        </script>

And the following js function:

  function aggiorna_data_nascosta() {
      var yearCurrent = now.getYear();
    var day=window.calendar.date.getDate();
    var month=window.calendar.date.getMonth();
    var year=window.calendar.date.getFullYear();
    
    var listYears=document.getElementById('anno');
    for (i=0; i<listYears.length; i++) {
        if (listYears.options[i].text == year) {
            document.getElementById('anno').selectedIndex=i;
        }
    }
    document.getElementById('giorno').selectedIndex=day-1;
    document.getElementById('mese').selectedIndex=month;
  }

The problem is the following:

When I open the page I see the date in the right encoding... for example "Venerdì 11 Gennaio 2019"... thanks to the PHP function "utf8_decode(data_attiva)" in the first line.

Unfortunately when I click the button with id "trigger" and I change the date the new date appears in the wrong encoding... for example it becomes "Venerdì 11 Gennaio 2019".

Can you suggest me a way to put the date to the right encoding when I change the date with javascript?

My current PHP header of the page is:

header('Content-Type: text/html; charset=iso-8859-1');

 

Share this post


Link to post
Share on other sites

The problem seems to be with the Calendar thing, not your code.

The best thing you could do, and what would be one way to fix your problem, would be to use UTF-8 instead of ISO 8859-1 for your pages.

Share this post


Link to post
Share on other sites
On 1/11/2019 at 5:46 PM, requinix said:

The problem seems to be with the Calendar thing, not your code.

The best thing you could do, and what would be one way to fix your problem, would be to use UTF-8 instead of ISO 8859-1 for your pages.

Yep sure but I am handling with an old app written in PHP 5 that has three different charsets (UTF-8, ASCII and ISO 8859-1). It's very complicated using UTF-8 instead of ISO 8859-1 for the pages (I have already tried it...).

I was wondering if it's possible to put some utf8-decode directly in the javascript code... and where. I am not very expert about Javascript...

Share this post


Link to post
Share on other sites
4 hours ago, michaelk said:

I was wondering if it's possible to put some utf8-decode directly in the javascript code... and where. I am not very expert about Javascript...

You probably could but I wouldn't recommend it.

This encoding this is going to hurt you over time. It'll be better if you buckle down and start fixing it now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.