我会再次问一些类似的问题我的问题开始关闭的风险(我无法删除旧的线程,因为它可能会让我被禁止,它说)..所以我需要解码一个我从HTML传递的ajax字符串表单到ajax和PHP.When我写英文并检查传递的值是否可以:
但是当我用西里尔语写一些时,这个“东西”被发送到php(单词是“ку”,与英语中的“ku”相同)
我试图从24小时内解决这个问题,并阅读了很多我不知所措并完全被阻止的信息。 所以再一次有我的代码:
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();
}
}
?>
简而言之,这是搜索引擎,如果数据库中存在与输入文本匹配则必须检查每个键,如果存在则显示它。 我的问题是:
如何解码发送给php的ajax信息,像往常一样显示西里尔字符而不是%D0%BA等等。
如果你们需要更多信息,请帮我随意提问。谢谢大家&lt; 3
答案 0 :(得分:1)
如果你的wand首先向服务器发送一些数据,你需要serialize
你的数据。
var request;
$("#foo").submit(function(event) {
// Prevent default posting of form - put here to work in case of errors
event.preventDefault();
// Abort any pending request
if (request) {
request.abort();
}
// setup some local variables
var $form = $(this);
// Let's select and cache all the fields
var $inputs = $form.find("input");
// Serialize the data in the form
var serializedData = $form.serialize();
// Let's disable the inputs for the duration of the Ajax request.
// Note: we disable elements AFTER the form data has been serialized.
// Disabled form elements will not be serialized.
$inputs.prop("disabled", true);
// Fire off the request to /form.php
request = $.ajax({
url: "/form.php",
type: "post",
data: serializedData
});
// Callback handler that will be called on success
request.done(function(response, textStatus, jqXHR) {
// Log a message to the console
console.log("Hooray, it worked!");
});
// Callback handler that will be called on failure
request.fail(function(jqXHR, textStatus, errorThrown) {
// Log the error to the console
console.error(
"The following error occurred: " +
textStatus, errorThrown
);
});
// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function() {
// Reenable the inputs
$inputs.prop("disabled", false);
});
});
<form method="post" accept-charset="UTF-8" id="foo">
<p>Търсене на храни: <input type="text" name="fsearch" id="food_search"></p>
</form>
<div id="food_search_result"></div>
PHP(即form.php):
$fsearch = isset($_POST['fsearch']) ? $_POST['fsearch'] : null;
然后尝试将此代码添加到html。 documentation about this
<head>
<meta charset="UTF-8">
</head>
答案 1 :(得分:0)
猜猜问题对我来说并不完全清楚,但我在问到问题的第二天就解决了问题。安德鲁的回答对于sumbit按钮来说真的很棒,但是现在我不会做任何按钮SOOO我修复了在$ connect和代码之后添加一行右边的问题:
$connect->set_charset("utf8");
问题不在于任何deconing,编码至少现在我看到当我输入类似“”或/ php返回\“\”或soo认为将来我将需要你的建议解码,编码所以感谢你们 ! &lt; 3我再一次用你的帮助解决了这个问题