0

I note a lot of questions asked about converting HTML characters such as greater/lesser than, ampersand, etc. to HTML entities, but I am not concerned with that. What I am having an issue with is:

  • JavaScript/Ajax code that passes values from an INPUT control that have character such as â to a PHP program -- in the PHP program I am storing the data using mysqli_real_escape_string() which if I weren't using Javascript/Ajax would do the trick.

  • It appears that before it gets to the PHP code, it is converted to something odd (two symbols) in the process (I fixed the data already, so can't tell you the exact two symbols, and doubt it is that important). I would like to have this converted to â which is the correct HTML Entity. However, I cannot find an answer out on the web that deals with this ...

I don't need every character in a string converted to HTML entities, just the higher end letters with diacriticals (accent symbols and such). I am hoping someone has a function for JavaScript that does this, but so far what I'm seeing searching the web does way too much (converting every character in a string to the HTML entity is not space efficient when storing it in a table).

JavaScript/Ajax code to obtain and pass data:

  function remembrance_save()
  {
     // first thing, clear out the message div used for this (if there's anything there):
     document.getElementById("remembrance_message").innerHTML = "";

     // from here we need to get the contents of all the various entry areas, and pass them
     // off to a PHP file and save the data ...
     // copy award data to session object by use of ajax/php:
     var namecode    = document.getElementById("namecode").value;
     // get value from TinyMCE ... not the DOM for JavaScript:
     var remembrance = tinymce.get('remembrance').getContent();
     var cl          = document.getElementById("commentor_link").value;
     var cn          = document.getElementById("commentor_name").value;
     $.ajax
     ({
        type: "POST",
        url: "<?php echo $History_html_RootPath; ?>Who/AjaxCalls/remembrance_save.php",
        data: { 'namecode' : namecode,
                'remembrance' : remembrance,
                'commentor_link' : cl,
                'commentor_name' : cn },
        //cache: false,
        success: function(data)
        {
           // need to see if we have an error, if so, display it, otherwise,
           // we should hopefully have success ...
           if ( data.toLowerCase().includes( "error" ) )
           {
              var errormsg = "<div class='alert alert-danger'>"+data+"</div>";
              document.getElementById("remembrance_message").innerHTML = errormsg;
              return;
           }
           else
           {
              // success!
              // change the message, and then clear out the values:
              var message = "<div class='alert alert-success'>";
                  message += data;
                  message += "</div>";
              document.getElementById("remembrance_message").innerHTML = message;

              // clear out values:
              // the remembrance editor takes two, because TinyMCE (editor)
              // stores the values its own way
              document.getElementById("remembrance").value = "";
              tinymce.get('remembrance').setContent( "" );
              // the other two
              document.getElementById("commentor_link").value = "0";
              document.getElementById("commentor_name").value = "";
              return;
           }
        }  // end success
     }); // end ajax call
  }; // end function remembrance_save()
Ken Mayer
  • 115
  • 15
  • 1
    Possible duplicate of [UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – CBroe Aug 10 '18 at 14:04
  • You should not convert this to HTML entities, but handle character encoding _correctly_. See duplicate for more info. – CBroe Aug 10 '18 at 14:05
  • Show us how exactly you are sending your data via AJAX, likely you simply neglected to handle it properly at that point. – CBroe Aug 10 '18 at 14:06
  • The UTF-8 encoding doesn't handle some of the characters I need, however latin-1 does (ISO-8859-1). Will add Ajax code above. – Ken Mayer Aug 10 '18 at 14:19
  • 1
    That’s nonsense, everything you got in ISO-8859-1 is covered in Unicode as well. – CBroe Aug 10 '18 at 14:20
  • I know you believe that, but when I use UTF-8 some of the characters I work with do NOT display properly. I do have *some* idea of what I'm doing here, I have run into issues with this over the last few years. I have a lot of international names that use a wide range of symbols. Please don't be condescending like this. Telling me "that is absolute nonsense" is not helpful. – Ken Mayer Aug 10 '18 at 14:22
  • 1
    _“but when I use UTF-8 some of the characters I work with do NOT display properly”_ - well, then you did not _handle_ UTF-8 properly along the whole chain. If you consider it “not helpful” when it gets pointed out that you are simply _wrong_, then I don’t know how we could possibly help you any further. Your initial example character was `â`, and if you have a problem with that in UTF-8, then you are **definitively** doing something wrong. – CBroe Aug 10 '18 at 14:26
  • Being rude is not helpful. Telling someone that what they posted is "nonsense" is not being helpful, it is being rude, and if you can't see that, it's your issue, not mine. – Ken Mayer Aug 10 '18 at 14:28
  • This very site here uses UTF-8, and if you check the source code, then you will see that there is just the `â` in the HTML for example at the position of your above _“that have character such as â to a PHP program”_ - that is the plain character, no HTML entities or anything else. – CBroe Aug 10 '18 at 14:28
  • If you are not going to help with my issue, then please stop ... hopefully someone with a more helpful attitude will be willing to post here. – Ken Mayer Aug 10 '18 at 14:31

0 Answers0