Jump to content

Mysterious Vanishing +


jasper182

Recommended Posts

I am currently working on a simple AJAX page that reads the value of a textarea and then writes that to my MySQL database everytime the contents of the textarea changes.  For some reason though, every character is stored in the database fine except for the +.  Whenever I type that, then check the view page any + that I have typed are gone.  I checked the database and they are not stored there either.  I pass the contents of the textarea to my update.php page which reads the new value and updates the record in the database.  Basically, the code on the update.php page is :

$newContent = addslashes($_POST['content']);
$query="UPDATE files SET Content='$newContent' WHERE FileID='1'";
$result=mysql_query($query);

The FileID=1 is just so I could test it by overwriting the same record.  The top line is the only time that I reference the data sent to update.php, so I don't know where it could be disappearing.  Is there something I'm missing?  Any direction would be greatly appreciated, and thanks in advance for sharing your knowledge with me.
Link to comment
Share on other sites

I'm simply javascript to call the update.php page.  Here is my function:

var OLDhttpvar4;
var httpvar4;
function getEditor() {
if(window.XMLHttpRequest){
httpvar4 = new XMLHttpRequest();
}
else
{
httpvar4 = new ActiveXObject("Microsoft.XMLHTTP");
}
var params;
params = "username=Jasper"
if (document.getElementById('mainEditor') != null) {
params = params + '&content=' + (document.getElementById('mainEditor').value);
}

httpvar4.open('POST', 'editorBox.php', true);
httpvar4.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

httpvar4.setRequestHeader("Content-length", params.length);
httpvar4.setRequestHeader("Connection", "close");

httpvar4.send(params);

httpvar4.onreadystatechange = function() {
if (httpvar4.readyState == 4) {

if(OLDhttpvar4 != httpvar4.responseText && httpvar4.responseText != 'No Print')
{
document.getElementById("contentBox").innerHTML = (httpvar4.responseText);
OLDhttpvar4 = httpvar4.responseText;
}
}
};


Also, I looked at the values being sent and the +'s are all getting to update.php.  Thanks for helping me out.
Link to comment
Share on other sites

"+" happens to be the url encoded value for space.  Could there be some url decoding going on (perhaps automatically)?

To debug it, I would check first that the + are being sent to update.php, then that the + are being received by update.php (using what jesirose suggested).  Once you've found where they are disappearing you can focus in on that.
Link to comment
Share on other sites

Ok, it looks like it is being sent, but like you said its not making it from my javascript to update.php.  Is there a way to send the Post variable as a string?  I tried putting it in quotes and that didn't work, it just added quotes around what I submitted.  Thanks for your assistance.
Link to comment
Share on other sites

Hmm.. what if you urlencode the "+" before sending it?  + encodes to %2B.  If you can send %2B from javascript and receive + in php, then all you'll need to do to fix it is to encode it before sending.

A good test would be to use your existing code but hardcode "content=%2B".
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.