dousi2553 2016-01-27 16:58 采纳率: 0%
浏览 94

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

I have a problem with special characters æøå when posting. I have searched the web, and cant fint anything thats related.

My AJAX looks like this:

    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'>";
}

What i have found out is that when it goes threw the JAVA it doenst post the special characters.

My PHP looks like this:

<?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;
    }




?>

And finally the page where it all comes from:

<?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>

The problem is, when i have written somthing in the textarea, and presses the button, it runs something through the AJAX, but Æ Ø Å doesnt get in the database. When i insert manually into the DB and reads from it it works like a charm.

I have tried to figure out the problem. But i can not figure it out.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 酬劳2w元求合作写文章
    • ¥15 在现有系统基础上增加功能
    • ¥15 远程桌面文档内容复制粘贴,格式会变化
    • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
    • ¥15 这种微信登录授权 谁可以做啊
    • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
    • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
    • ¥15 网络设备配置与管理这个该怎么弄
    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图