开发者

how can save a string as it is in mysql database

Im trying to save this string:

~`@#$%^&*()_+}{":?><,./;'[]=-|\ 

using a AJAX call in ph开发者_JAVA百科p. But in the database it saves as this:

~`@#$%^????

this is my AJAX call

function saveComment(timesheetId,activityId,date,comment,employeeId) {

    var r = $.ajax({
        type: 'POST',
        url: commentlink,
        data: "timesheetId="+timesheetId+"&activityId="+activityId+"&date="+date+"&comment="+comment+"&employeeId="+employeeId,
        async: false
    }).responseText;

    return r;
}

Edit: Fixed display of strings and code.


You need to in javascript call encodeURIComponent on the string with the weird characters before you send it to the server.

EDIT: Tomalak pointed out a better method.


If you want to put a variable 'text' in the data, you should run it through $.URLEncode(text) before doing so; as it is, the '&' character in the text introduces a new parameter.


jQuery supports an object as the data parameter in Ajax requests. This also does the URL encoding for you automatically:

$.ajax({
  type: 'POST',
  url: commentlink,
  data: {
    timesheetId: timesheetId, 
    activityId:  activityId, 
    date:        date, 
    comment:     comment, 
    employeeId:  employeeId
  },
  success: function (data) {
    alert(data);
  }
});

Also - you should never use synchronous Ajax requests. Always work with callback functions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜