PHP,AJAX,TINYmce POST与特殊字符ÆØÅ无法正常工作

时间:2016-01-27 16:58:21

标签: php jquery mysql ajax tinymce

发帖时我遇到特殊字符æøå的问题。 我在网上搜索过,并且无法找到任何相关内容。

我的AJAX看起来像这样:

    function savep(str){
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "savep.php";
    var text = document.getElementById("myarea").value=tinyMCE.activeEditor.getContent();
     var vars = "id="+str+"&text="+text;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState === 4 && hr.status === 200) {
            var return_data = hr.responseText;
            document.getElementById("status").innerHTML = return_data;
        }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
//    document.getElementById("status").innerHTML = "processing...";
    document.getElementById("status").innerHTML = "<img src='../pics/process.gif' width='32' height='32'>";
}

我发现的是,当它扔掉JAVA时它会发布特殊字符。

我的PHP看起来像这样:

<?php
include('../session/session.php');



if(isset($_POST['id'])&& !empty($_POST['text'])){


 $id = $_POST['id'];
 $cont = $_POST['text'];
       error_log($cont);
    $stmt = $conn->prepare("UPDATE pages SET content=:cont where id=:id");
    $stmt->bindParam(':cont',$cont, PDO::PARAM_STR);
    $stmt->bindParam(':id',$id, PDO::PARAM_INT);
    $stmt->execute();
    $pchange = $stmt->rowCount();

    if($pchange > 0)
    {

        echo "<div class='alert alert-success' role='alert'>Adgangskoden er ændret</div>";
          $conn = null;
    } else
    {
        echo  "<div class='alert alert-danger' role='alert'>Adagangskoden blev ikke ændret</div>";
          $conn = null;
    }

    } else
    {

         echo  "<div class='alert alert-danger' role='alert'>Det indstastede kodeord er forkert!</div>";
          $conn = null;
    }




?>

最后是所有来自的页面:

<?php
include ("../session/session.php");
include ("../include/dbconn.php");
if(isset($_GET['id'])&& !empty($_GET['id']))
{
    $id = $_GET['id'];



    $stmt = $conn->prepare("SELECT * FROM pages WHERE id= :id ");
    $stmt->bindParam(':id',$id, PDO::PARAM_INT);
    $stmt->execute();
    $edit = $stmt->fetchAll();

   foreach($edit as $row)
   {
        $title = $row['title'];
        $cont = $row['content'];
       echo $title;
   }    


}
?>


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
     <link rel="icon" href=".../pics/favicon.ico">

    <title>Dashboard Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="../css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="../css/dashboard.css" rel="stylesheet">
    <script src='//cdn.tinymce.com/4/tinymce.min.js'></script>
     <script src="../js/userfunc.js"></script>
      <script>

  tinymce.init({
     selector: '.text2',
    theme: 'modern',
    width: "800",
    height: "300",
  plugins: [
    'advlist autolink lists link image charmap print preview hr anchor pagebreak',
    'searchreplace wordcount visualblocks visualchars code fullscreen',
    'insertdatetime media nonbreaking save table contextmenu directionality',
    'emoticons template paste textcolor colorpicker textpattern imagetools'
  ],
  toolbar1: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
  toolbar2: 'print preview media | forecolor backcolor emoticons',
  image_advtab: true

  });
  </script>

  </head>

  <body>
        <?php include ("navbar.php") ?>

        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header">Edit <?php echo $title; ?></h1>

              <div class="col-md-8">
                  <div class="well">
                      <textarea class='text2' id='myarea'> <?php echo $cont; ?> </textarea> <br>
               <button  name='save' id='save' class='btn btn-success' onclick="savep(<?php echo $id; ?>)" > Save </button>

               <div id="status">

                  </div>  
              </div>


        </div>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <!-- Just to make our placeholder images work. Don't actually copy the next line! -->
    <script src="../js/holder.min.js"></script>

  </body>
</html>

问题是,当我在textarea中写了somthing并按下按钮时,它会通过AJAX运行一些东西,但是ÆØÅ并没有进入数据库。当我手动插入数据库并从中读取时,它就像一个魅力。

我试图找出问题所在。但我无法弄明白。

0 个答案:

没有答案