0

I hace the following code-Snippeletsenter image description here

  1. I have an &-character in a HTML(PHP)-form [see Graphics]

I send this data to a Javascropt-file by a button

<button onclick="sendEntry()" > Senden </button>

sendEntry is defined as follows:

....

  if(document.getElementById("Eintrag").value==""){
        ERROR= ERROR + "Kein Eintrag eingegeben\n";             
    }
    else{
        **PARAMS=PARAMS** + "&Eintrag=" + document.getElementById("Eintrag").value; 
     }

...

the result is sent to a Ajax-request:

ajaxRequest.open("POST", "./../php/MakeEntry.php", true);
    ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajaxRequest.send(PARAMS);

....

How can I avoid that the &-char auses a new POST-Parameter as shown in the Graphics ??

  • You can encode your data and send , this link will help you for JavaScript encode http://stackoverflow.com/questions/18279141/javascript-string-encryption-and-decryption – Elby Oct 04 '16 at 08:34

2 Answers2

0
ajaxRequest.open("POST", "./../php/MakeEntry.php", true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.send(encodeURI(PARAMS));

Try to encode your data ;)

Steeve Pitis
  • 4,283
  • 1
  • 21
  • 24
0

You need to encode the individual values that you are sending in the url so that they will not be interpreted as special characters:

else{
    **PARAMS=PARAMS** + "&Eintrag=" + encodeURIComponent(document.getElementById("Eintrag").value); 
 }

Note that this applies to all values, the example is just for the last one you are adding.

jeroen
  • 91,079
  • 21
  • 114
  • 132