tim
tim

Reputation: 41

how can save a string as it is in mysql database

Im trying to save this string:

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

using a AJAX call in php. 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.

Upvotes: 1

Views: 168

Answers (3)

Joe
Joe

Reputation: 82624

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.

Upvotes: 2

Tomalak
Tomalak

Reputation: 338316

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.

Upvotes: 0

boycy
boycy

Reputation: 1493

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.

Upvotes: 0

Related Questions