douyan2002 2017-02-16 09:48
浏览 51
已采纳

如何解码传递给PHP的西里尔字母ajax字符串

I will ask again something similarly with risk of my question begin closed(i cannot delete the old thread,because it might get me banned,it says)..So i need to decode an ajax string which i am passing from HTML form to ajax and on to PHP.When i write in English and check values which is passed it is all right:

Example1

But when i write some in cyrillic this 'thing' is sended to php (word is "ку",same as "ku" in english)

Example 2

I am trying to fix this problem from like 24 hours and read SO much information that i got overwhelmed and totally blocked. So once again there is my code:

jQuery(document).ready(function ($) {
    new InputStreamReader(conn.getInputStream(), "UTF-8")); 
    $("#food_search").keyup(function(event){
        var search_term =$(this).val();
$.ajax({
    type:"POST",
    url:"http://test.com/bg/%D1%82%D1%8A%D1%80%D1%81%D0%B5%D0%BD%D0%B5-%D0%BD%D0%B0-%D1%85%D1%80%D0%B0%D0%BD%D0%B8/",
    data:{'fsearch':search_term},
    success:function(res){
        $("#food_search_result").html(res);
        console.log(res);
    },
    error: function (xhr, ajaxOptions, thrownError) {
           alert(xhr.status);
           alert(xhr.responseText);
           alert(thrownError);
       }
});
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!----------------------------------------------------------------
                              HTML
----------------------------------------------------------------->
<form method="post" accept-charset="UTF-8">
<p>Търсене на храни: <input type="text" name="fsearch" id="food_search"></p>
</form>
<div id="food_search_result"></div>
<!----------------------------------------------------------------
                              PHP
----------------------------------------------------------------->
<?php
$hostname = "localhost";
$username = "gosho";
$password = "0!ijgls9df";
$databaseName = "dbName";
$connect = new mysqli($hostname, $username, $password, $databaseName);
$fsearch="";

if(!empty($_POST['fsearch'])) {
$fsearch = $_POST['fsearch'];

$req = $connect->prepare("SELECT title FROM food_data_bg WHERE title LIKE ?");
$value = '%'.$fsearch.'%';
$req->bind_param('s', $value);
$req->execute();
$req->store_result();
$num_of_rows = $req->num_rows;
$req->bind_result($title);
if ($req->num_rows == 0){

echo 'Няма резултати';
}
else{
while($data=$req->fetch()){
   ?>
       <div class="search-result">
           <span class="result-title"><?php echo $title; ?></span>
       </div>
       <?php
       }
var_dump($_POST['fsearch']);
$req->free_result();
    }
}
?>

So for short this is Search engine which must check on every key up if there is a match in database with the inputed text and display it if there is. And my question is:

How to decode the ajax information sended to php to display cyrillic characters as usual and not like %D0%BA and so on..

If you guys need additional info,to help me feel free to ask.Thank you all <3

</div>
  • 写回答

2条回答 默认 最新

  • dqtiober37522 2017-02-20 19:14
    关注

    Guess the problem wasn't totally clear for me,but i fixed the problem the day after i asked the question.Andrew's answer is really awesome for sumbit button,but for now i won't make any button SOOO i fixed the problem by adding a single line of right AFTER the $connect and the code is:

    $connect->set_charset("utf8");
    

    The problem wasn't with any deconing,encoding atleast for now i saw that when i type like "" or / php returns \"\" or soo think for future i will need an of your advices for decoding,encoding so thank you guys ! <3 Once again i solved the problem with your help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!