Jean Smith
Jean Smith

Reputation: 43

javascript - post send textarea to php but "+" converts to space

I am using this code to send textarea value to php

  function SendTextArea() {
      var hr = new XMLHttpRequest();
      var url = "process.php";
      var fn = document.getElementById("textarea1").value;
      var vars = "txt=" + fn;
      hr.open("POST", url, true);
      hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      hr.onreadystatechange = function() {
          if (hr.readyState == 4 && hr.status == 200) {
              var return_data = hr.responseText;
              document.getElementById("state").innerHTML = return_data;
          }
      }
      hr.send(vars);
      document.getElementById("state").innerHTML = "wait";
  }

It is working well, but I have a problem the problem is: when textarea contains "+", the "+" converts to space " "

  <?php
  $text = $_POST["txt"];
  echo "$text";
  ?>

Upvotes: 4

Views: 166

Answers (1)

Brijesh Bhatt
Brijesh Bhatt

Reputation: 3830

You can use encodeURIComponent() function for this problem:

var vars = "txt=" + encodeURIComponent(fn);

Upvotes: 4

Related Questions