Link to home
Start Free TrialLog in
Avatar of Wayne Barron
Wayne BarronFlag for United States of America

asked on

Classic ASP insert Accented Characters from Czech do not insert with the ř

Hello All.

Working with Classic ASP, and trying to insert accented characters like this do not work. Dvořák Hall.

Textarea, insert or update data in a SQL Server database.
SQL Server info

Column   = CodeDesc (NVarChar (MAX))
Collation = Latin1_General_100_CI_AI

Open in new window


Textarea = Name "theEditor"
Update statement

upCode.commandtext = "update Column set CodeDesc=? where ID=?"
upCode.Parameters.Append upCode.CreateParameter("@CodeDesc", adLongVarWChar, adParamInput, 350, strCode)

Open in new window

I even tried with Cast, and it still will not insert.

CodeDesc=cast(? as nvarchar(max))

Open in new window

When I try to send this name
Dvořák Hall 
It inserts as
Dvorák Hall 
Missing the ř 

If I embed the name like this.

strCode = "Dvořák Hall "

Open in new window

It inserts the record perfectly.
But not when I have it in a textarea and make the call.


strCode = request.Form("theEditor")

Open in new window


Any assistance on this would be great.
Thank you.
Wayne

Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Add these to your asp page
Response.CharSet = "UTF-8"
Response.Codepage = 65001

Open in new window

Avatar of Wayne Barron

ASKER

Hey, David.

This code
Response.CodePage = 28591
Response.CharSet = "ISO-8859.1"

Open in new window

Gives me this response. (No ř)
Dvorák Hall

Open in new window

This code
Response.CharSet = "UTF-8"
Response.Codepage = 65001

Open in new window

Gives me this response (No á)
Dvoř�k Hall

Open in new window

The issue when your ASP pages, does it allow the user to customize their Preferences and define their character-set/type?

You could look at encoding the data in the form, what is the form post and defines the ?
you can look at the iso-8859.2 this will cover CZ characters.
potentially adjust your processing based on the source of the data.
As I stated in the other thread, I am the only person inserting records at the current time.
The site is a media-related website, where I insert data regarding the music recording sessions.
It can be a studio or venue in another country, and I need to insert that data into the database for that music.
Dvořák Hall is a concert hall in Prague.

See how it shows up correctly when we write it here on EE?
How is that being accomplished here?
The issue nor the question is who is entering, the issue.is whether the entry itself is correctly mapped.
In your browser change the language/character set to utf-8 and see whether that solves the encoding issue.

Which browser are you using?
Can you look at the raw http data your code receives?
Google Chrome and Edge
As for the raw data.
I had never done that method before, so that is something new, and for that, thank you.


With this at the top, before the Raw data code to post.
Response.ContentType = "text/html"
Response.AddHeader "Content-Type", "text/html;charset=UTF-8"
Response.CodePage = 65001
Response.CharSet = "UTF-8"

Open in new window

And then the Raw Data to post
If Request.TotalBytes > 0 Then    
    Response.ContentType = "text/plain"
    Response.Write "Your " & Request.ServerVariables("REQUEST_METHOD") & " data was: " 
    Response.BinaryWrite Request.BinaryRead(Request.TotalBytes)
End If

Open in new window

The output is
theEditor=Dvo%u0159%E1k%20Hall
Open a text editor, while holding alt key enter 0159 on your numbers keypad
This should reflect the R in the font in use
The %E1 is the ascii A with %20 ascii space.

One option is to run through replacing the ascii characters first
Yea, I had checked in on the u0159
Which is the r with the accent over it. (LATIN SMALL LETTER R WITH CARON)
And the %E1 = LATIN SMALL LETTER A WITH ACUTE
I tried doing this but it did not work.

strGet = request.form("TheTextArea")
replace(strGet,"%E1","á")

Open in new window

And I also tried this
replace(strGet,"%E1","á")

Open in new window


In another processing code for this site, I am uploading MP3 files, which I've loaded with all the information about each song.
Using the right component, I uploaded the file and obtained all accented data.
But that does not seem to be the case coming from a text area, as we have found out here in this thread.

I've looked over several forums and found all the following, and my pages and IIS are defaulted as such.


IIS = Code Page 0
All physical files are UTF-8

All appropriate headers are in place.

I've tried
Server.HTMLEncode(strGet)

Open in new window

And this does not work either.


0159 in notepad gives me Ÿ
My error, thought the u0159 would map to the keyboard mapping classification.

UTF-8 character map.
https://www.fileformat.info/info/charset/UTF-8/list.htm
Not sure whether you can handle the ASCII encoded first, and then deal with the UTF-8 conversion to preserve the ascii characters in the string.
Do you know of a way to capture it and convert it?
What are you setting your ASP pages to dealing with charset?
one option is to set it on each page to be utf-8 versus let the browser adjust based on user input.

See if this helps.

https://www.w3schools.com/html/html_charset.asp

If you use templates, adding the meta within ..
To set the browser enter UTF encoded text.

test is whether the accented á alt+0225
Main page has
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

The ASP Processing pages have
<%
Response.ContentType = "text/html"
Response.AddHeader "Content-Type", "text/html;charset=UTF-8"
Response.CodePage = 65001
Response.CharSet = "UTF-8"
%>

Open in new window

Another thought are you setting a charset  as part of the definition of the form
Accept-charset="UTF-8"

Https://www.w3schools.com/tags/att_form_accept_charset.asp

Try the above

It seems the issue has been narrowed to how to deal with data that includes a mix of utf-8 and high-ascii encoding within the same string.
Yea, I had already tried that.
The page loads with the word without an issue.
When I submit the form, it messes up the Characters.
I decided to check something, and it worked.

I removed the AJAX from the page used and sent the test page to the 2.asp page, and it worked.
So, it seems the issue is my AJAX script is not supporting codepage.

This is the output 2.asp.
<%
Response.ContentType = "text/html"
Response.AddHeader "Content-Type", "text/html;charset=UTF-8"
Response.CodePage = 65001
Response.CharSet = "UTF-8"

'If Request.TotalBytes > 0 Then    
'    Response.ContentType = "text/plain"
'    Response.Write "Your " & Request.ServerVariables("REQUEST_METHOD") & " data was: " 
'    Response.BinaryWrite Request.BinaryRead(Request.TotalBytes)
'End If
strGet = request.Form("theEditor")
response.Write strGet
%>

Open in new window


Here is the example code that works.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form accept-charset="utf-8"  action="2.asp" method="post" >
<textarea name="theEditor" rows="5" cols="20">Dvořák Hall</textarea>
<br />
<input type="submit" name="Submit" style="width:170px;" value="Code" />
</form>
</body>
</html>

Open in new window


Here is the example code which does not work.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" charset="UTF-8">
function xmlhttpPost(strURL,formname,responsediv,responsemsg) {
    var xmlHttpReq = false;
    var self = this;
    // Xhr per Mozilla/Safari/Ie7
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // per tutte le altre versioni di IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
	self.xmlHttpReq.setRequestHeader("charset", "utf-8");
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
			// Quando pronta, visualizzo la risposta del form
            updatepage(self.xmlHttpReq.responseText,responsediv);
        }
		else{
			// In attesa della risposta del form visualizzo il msg di attesa
			updatepage(responsemsg,responsediv);

		}
    }
    self.xmlHttpReq.send(getquerystring(formname));
}
function getquerystring(formname) {
    var form = document.forms[formname];
	var qstr = "";

    function GetElemValue(name, value) {
        qstr += (qstr.length > 0 ? "&" : "")
            + escape(name).replace(/\+/g, "%2B") + "="
            + escape(value ? value : "").replace(/\+/g, "%2B");
			//+ escape(value ? value : "").replace(/\n/g, "%0D");
    }
	var elemArray = form.elements;
    for (var i = 0; i < elemArray.length; i++) {
        var element = elemArray[i];
        var elemType = element.type.toUpperCase();
        var elemName = element.name;
        if (elemName) {
            if (elemType == "TEXT"
                    || elemType == "TEXTAREA"
                    || elemType == "PASSWORD"
					|| elemType == "BUTTON"
					|| elemType == "RESET"
					|| elemType == "SUBMIT"
					|| elemType == "FILE"
					|| elemType == "IMAGE"
                    || elemType == "HIDDEN")
                GetElemValue(elemName, element.value);
            else if (elemType == "CHECKBOX" && element.checked)
                GetElemValue(elemName, 
                    element.value ? element.value : "On");
            else if (elemType == "RADIO" && element.checked)
                GetElemValue(elemName, element.value);
            else if (elemType.indexOf("SELECT") != -1)
                for (var j = 0; j < element.options.length; j++) {
                    var option = element.options[j];
                    if (option.selected)
                        GetElemValue(elemName,
                            option.value ? option.value : option.text);
                }
        }
    }
    return qstr;
}
function updatepage(str,responsediv){
    if( 'REDIRECT-TO: '==String(str).substring(0,13).toUpperCase() )
      top.location.href=str.substring(13);
    else
        document.getElementById(responsediv).innerHTML = str;
}
</script>
</head>

<body>
<form accept-charset="utf-8" style="width:100%;" name="Test" action="2.asp" method="post" onsubmit="xmlhttpPost('2.asp', 'Test', 'CodeDiv', '<img src=\'graph/pleasewait.gif\'>'); return false;">
<textarea name="theEditor" rows="5" cols="20">Dvořák Hall</textarea>
<br />
<input type="submit" name="Submit" style="width:170px;" value="Code" />
</form>
<div id="CodeDiv"></div>
</body>
</html>

Open in new window

OK. This AJAX works.
But I cannot get the existing one to work, which is needed for the Redirect portion of it.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax form submit without reloading page</title>
<body>
<h1>This is the simple form using Ajax</h1>
<form id="myForm">
	<div>
	<label>Name</label>
	<input type="text" id="theEditor" name="theEditor" value="Dvořák Hall">
	</div>
	<button type="submit">Submit</button>
</form>
<div id="postData"></div>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(responsediv){
    $('#myForm').submit(function(e){
        e.preventDefault();
        $.ajax({
            url: "2.asp",
            type: "POST",
            data: $(this).serialize(),
            success: function(data){
                $("#postData").html(data);
            },
            error: function(){
                alert("Form submission failed!");
            }
        });

    });
});
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Wayne Barron
Wayne Barron
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Updated the code to accept any form, no worries on the ID tag.
Change this
$('#myForm').submit(function(e){

Open in new window

to this
$('form').submit(function(e){

Open in new window

Thanks, Arnold, for the "raw http data."
If it were not for that one piece of information, I would not have known about doing that, which will now be utilized in my coding from here on out.

David. As always, thanks for your input.
I forgot to add the "CharSet" and "CodePage" to the processing page.
Even though it did not resolve the issue, it turned out it was my AJAX script, which I've been using since 2011, that was causing the problem.

With the new AJAX, I will now be able to do what needs to be done in my Classic ASP and VB.NET projects.

Thanks, guys.
Wayne
That original ajax code looked a bit old, and you can still modernize what you have some more but glad it is working.

For trouble shooting, I might have just tried a very simple page without javascirpt where you post directly to another page and that page just does a response write of the posted data.


Hey, Scott.
I did exactly what you stated, which is why I looked at the ajax page.
If the code response worked on a regular page, it should work on the ajax.
But yea, that code is right old. I have been using it since around 2011, so 10 years.
I was having a little of a time getting the new script to work with submitting, and then I had to look at how I am submitting the form in the Admin section of the site, and it is looking at a hidden field.
<input type="hidden" id="theTruth" name="theTruth" value="" />

Open in new window

Which is fine, so I just added this into the demo page and it works like a charm.
<input type="submit" name="Submit" tabindex="3" style="width:150px;" value="Code" id="Update" onClick="document.getElementById('theTruth').value='Code';" />
<input type="hidden" id="theTruth" name="theTruth" value="" />

Open in new window


I will continue to play around with it, but I am just glad that I can pass accented characters to the processing page and then into the database without losing any data.

Have a good one, Scott.