javascript - 发送textarea到php,但" +"转换为空间

时间:2015-05-18 05:35:36

标签: javascript php jquery html

我正在使用此代码将textarea值发送到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";
  }

运作良好,但我遇到了问题 问题是:当textarea包含" +"时," +"皈依空间" "

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

1 个答案:

答案 0 :(得分:4)

您可以使用encodeURIComponent()功能解决此问题:

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