Working with Javascript and Ajax is new to me. When I try to submit data containing the '&' character to the database for live updates, the string gets cut off after that symbol. After testing it using the alert function, I can see the full string but am unsure how to resolve this issue.
I attempted to replace the '&' character with &
, but it did not work. Below are the lines of code I used to extract the values and the replacement attempt. Note that I'm using nicEditor, so the first line of code is necessary to retrieve text from the specific field where the content is stored within the editor.
var boxval = $('#statusContentForum').find('.nicEdit-main').text();
var dataString = 'statusContent='+ boxval;
var dataString = dataString.replace(/&/g, '&');
var dataString = dataString.replace(/\|&;\$%@"<>\(\)\+,/g, "");
I tested these characters:
abcdefghijklmnopqrstuvwxyz 1234567890-=`~!@#$%^&*()_+{}[]|\"':;,<.>?/
The result was:
abcdefghijklmnopqrstuvwxyz 1234567890-=`~!@#$%^
Has anyone else experienced this issue before? Any assistance would be greatly appreciated. Feel free to ask for more code if needed.
*********UPDAT FULL CODE *********
Below is the entire code including JS/AJAX:
$(function() {
//Update Message...
$(".update_button").click(function() {
//var boxval = $("#statusContent").val();
var boxval = $('#statusContentForum').find('.nicEdit-main').text();
var dataString = 'statusContent='+ boxval;
//var dataString = dataString.replace(/>/g, '>');
//var dataString = dataString.replace(/</g, '<');
var dataString = dataString.replace(/&/g, '&');
var dataString = dataString.replace(/\|&;\$%@"<>\(\)\+,/g, "");
if(boxval=='') {
alert("Please Enter Some Text");
} else {
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="images/ajax-loader.gif" align="absmiddle"> <span class="loading">Loading Comment...</span>');
// alert(dataString);
// throw new Error("Something went badly wrong!");
$.ajax({
type: "POST",
url: "ajax/statusPost.php",
data: dataString,
cache: false,
success: function(html){
$("ol#update").prepend(html);
$("ol#update li").slideDown("slow");
var clearVal = nicEditors.findEditor("statusContent");
clearVal.setContent("");
$("#flash").hide();
}
});
} return false;
});
});
If you require the HTML, here it is:
<form name="newstatus" id="statusContentForum" action="profile.php" method="post" class="commentForm">
<textarea name="statusContent" id="statusContent" placeholder="What's on your mind?"></textarea>
<input type="submit" value="Update" name="submit" class="update_button"/>
</form>