Question

I'm using a fairly simple and standard jQuery post to post data to another page. Both pages have

<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<% Response.CharSet = "UTF-8" %>

The function part of the jQuery post just has alert(data) and all that I return is the value of one form field enclosed in brackets. So when I add a British pound sign (£) it alerts [£] accordingly

However, I also write to a text file on the page I post to and there it actually writes £ rather than just £

So when I do my database insert, it actually inserts £ as well rather than just £

Any reason why this is happening?

Page 1

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Intelligence Point </title>
    <meta name="description" content="">

<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="">

<!-- Bootstrap core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="/css/custom.css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Shadows+Into+Light' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]--> 
    <style type="text/css">        p,textarea {
            padding: 5px;
        }
        textarea {
        border:1px solid #ccc;}
    </style>
</head>
<body>


    <div class="container" style="margin-top:-30px;">
        <p>&nbsp;</p>

    <p class="bg-primary">Comments last updated by JJ004 on 2014-05-07 at 16:28</p>

    <p><textarea class="bg-success" maxlength="4000" style="width:100%;" rows="4" wrap="soft" id="comment1">£</textarea></p>
    <p><textarea class="bg-success" maxlength="4000" style="width:100%;" rows="4" wrap="soft" id="comment2">£</textarea></p>
    <p><textarea class="bg-success" maxlength="4000" style="width:100%;" rows="4" wrap="soft" id="comment3">£</textarea></p>
    <p><textarea class="bg-success" maxlength="4000" style="width:100%;" rows="4" wrap="soft" id="comment4">£</textarea></p>

    <button type="button" class="btn btn-success" id="postcomment">Save comments</button>
    </div>


    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>



    <script type="text/javascript">
        $(document).ready(function () {
            window.resizeTo(800, 750);
            $("#postcomment").click(function () {
                $.post(
                   "putComment.asp",
                   {
                       yR: "123",
                       yP: "456",
                       yN: "",
                       yF1: "XXX",
                       yF2: "YYY",
                       yF3: "ZZZ",
                       yF4: "",
                       yC1: $('#comment1').val(),
                       yC2: $('#comment2').val(),
                       yC3: $('#comment3').val(),
                       yC4: $('#comment4').val()
                   },

                   function (data) {

                       //alert(data);
                       var url = "SOMEWHERE.COM";
                       alert("Narrative saved");
                       window.opener.location.href = url;
                       window.close(); // or self.close();

                   }

                );
            });
        });
    </script>

</body>
</html>

putComment.asp

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <% Response.CharSet = "UTF-8" %>
</head>
<body>

<!--#include virtual="/comments/connProd.inc"-->
<%
Function cleanSQL(thisWord)
       Dim newWord
       If thisWord <> "" Then
                       newWord = Replace(thisWord,"/*","")
                       newWord = Replace(newWord,"*/","")
                       newWord = Replace(newWord,"UNION","")
                       newWord = Replace(newWord,"'","''")
                       newWord = Replace(newWord,"""","&amp;quot;")
                       newWord = Replace(newWord,"<script>","[script]")
                       newWord = Trim(newWord)
       End If
       cleanSQL = newWord
       End Function

       Function in_array(element, arr)
    For i=0 To Ubound(arr) 
        If Trim(arr(i)) = Trim(element) Then 
            in_array = True
            Exit Function
        Else 
            in_array = False
        End If  
    Next 
End Function

zR=cleanSQL(request.form("yR"))
zP=cleanSQL(request.form("yP"))
zN=cleanSQL(request.form("yN"))

zC1=cleanSQL(request.form("yC1"))
zC2=cleanSQL(request.form("yC2"))
zC3=cleanSQL(request.form("yC3"))
zC4=cleanSQL(request.form("yC4"))

zF1=cleanSQL(request.form("yF1"))
zF2=cleanSQL(request.form("yF2"))
zF3=cleanSQL(request.form("yF3"))
zF4=cleanSQL(request.form("yF4"))

    'response.Write "["&zC1 & "]<br/>"

sql="insert into IPPR.Comments values("
sql=sql+"'"+zR+"',"
sql=sql+"'"+zP+"',"
sql=sql+"'"+zF1+"',"
sql=sql+"'"+zF2+"',"
sql=sql+"'"+zF3+"',"
sql=sql+"'"+zF4+"',"
sql=sql+"'"+zC1+"',"
sql=sql+"'"+zC2+"',"
sql=sql+"'"+zC3+"',"
sql=sql+"'"+zC4+"',"
sql=sql+"getdate(),"
sql=sql+"'"+zN+"')"


dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject") 
set f=fs.OpenTextFile("c:\inetpub\wwwroot\text\log.txt",8,true)
f.WriteLine("")
f.WriteLine("["&zC1 & "]" & sql)
f.close
set f=nothing
set fs=nothing


    'response.write sql
    'response.flush
set rs=conn.execute(sql)

%>
    </body>
</html>
Was it helpful?

Solution 2

Response.ContentType = "text/html" 
Response.AddHeader "Content-Type", "text/html;charset=UTF-8" 
Response.CodePage = 65001 
Response.CharSet = "UTF-8"

Added this and was fine!

OTHER TIPS

Note:

It's really difficult to convey code in comments so I'm writing this.

As I have explained your getting a mismatch in encoding because you are missing key steps, try this;

  1. Add the following lines to the beginning of Page 1 and putComments.asp

    <%@Language="VBScript" CodePage = 65001 %>
    <%
    Response.CodePage = 65001
    Response.CharSet = "UTF-8"
    %> 
    
    • First line tells IIS to process the content of the page as UTF-8.

    • Second line instructs all Response strings to be encoded as UTF-8.

    • Third line is the equivalent of setting ;Charset=UTF-8 in Content-Type header.

  2. Make sure all ASP files in the chain (including any #include files) are saved with UTF-8 encoding otherwise IIS processing the files as UTF-8 when they are not will cause encoding mismatches.

  3. Any data posted should be sent as UTF-8 this differs depending on the mechanism in your case you're using a JQuery Ajax Post (uses UTF-8 by default) so no change should be needed here.


Useful links

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top