0

This is driving me crazy!

I have something like this in JS:

xmlhttp.open("POST", "/note.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
console.log(" >>>> " + note  + " " + noteHTML);
xmlhttp.send("what=edit&note=" + note + "&noteHTML=" + noteHTML + "&noteIdx=" + noteIdx);

Output in console:

 >>>> C++ C++

This code sends most of the text correctly through, aka in note.php I get the same content than the content on the JS side, but for whatever reasons it does something very strange to the string C++. When note or noteHTML which are strings are set to C++ then what arrives on the PHP side (in note.php) is C and not C++!! This doesn't make ANY sense to me. Maybe there is something wrong with the encoding I chose. I tried html and plain/text but for whatever reason I don't even get anything at all on the php side so I gave up with looking into this direction any further. Any idea why?

else if ($_POST['what'] == "edit")
{
    printArray($_POST);
}

Output in browser:

what => edit note => C noteHTML => C noteIdx => 17no

WHY?

user18490
  • 3,546
  • 4
  • 33
  • 52

1 Answers1

0

You have to change it like this (use encodeURIComponent() on each element):

xmlhttp.open("POST", "/note.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
console.log(" >>>> " + note  + " " + noteHTML);
xmlhttp.send("what=edit&note=" + encodeURIComponent(note) + "&noteHTML=" + encodeURIComponent(noteHTML) + "&noteIdx=" + encodeURIComponent(noteIdx));

https://stackoverflow.com/a/4276396/4916265

Community
  • 1
  • 1
JustOnUnderMillions
  • 3,741
  • 9
  • 12