这个有效,但没有将结果分开:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Get datas from database where (ID = value of input) and get back the datas into different textareas.</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
ID number: <input type="text" id="searchid" ><br>
Result Name: <textarea id="resultname"></textarea><br>
Result Year: <textarea id="resultyear"></textarea><br>
Result Type: <textarea id="resulttype"></textarea><br>
<script>
$(document).ready(function() {
$('#searchid').keydown(function (e){ // Event for enter keydown.
if(e.keyCode == 13){
var idvalue = $("#searchid").val(); // Input value.
$.ajax({ //Ajax call.
type: "GET",
url: "search.php",
data: 'id=' + idvalue ,
success: function(msg){
// Show results in textareas.
$('#resultname').html(msg.name);
$('#resultyear').html(msg.year);
$('#resulttype').html(msg.type);
}
}); // Ajax Call
} //If statement
}); //event handler
}); //document.ready
</script>
</body>
</html>
<?php
if ($_GET['id']):
// Connect to database.
$con = mysqli_connect("localhost","Krisz","password");
mysqli_select_db ($con,'coin');
// Get the values from the table.
$sql = "SELECT Name, Year, Type FROM main_db where ID = $_GET[id] ";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($result))
{
echo "$row[Name]";
echo "$row[Year]";
echo "$row[Type]";
}
endif;
?>
我在这里试过json。我确定有很多错误。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Get datas from database where (ID = value of input) and get back the datas into different textareas.</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#searchid').keydown(function (e){ // Event for enter keydown.
if(e.keyCode == 13){
var idvalue = $("#searchid").val(); // Input value.
$.ajax({ //Ajax call.
type: "GET",
url: "search.php",
data: 'id=' + idvalue ,
type: 'json',
success: function(msg){
// Show results in textareas.
$('#resultname').html(msg.name);
$('#resultyear').html(msg.year);
$('#resulttype').html(msg.type);
}
}); // Ajax Call
} //If statement
}); //event handler
}); //document.ready
</script>
</head>
<body>
ID number: <input type="text" id="searchid" ><br>
Result Name: <textarea id="resultname"></textarea><br>
Result Year: <textarea id="resultyear"></textarea><br>
Result Type: <textarea id="resulttype"></textarea><br>
</body>
</html>
<?php
if ($_GET['id']):
$dataid = json_decode($_GET['id']);
// Connect to database.
$con = mysqli_connect("localhost","Krisz","password");
mysqli_select_db ($con,'coin');
// Get the values from the table.
$sql = "SELECT Name, Year, Type FROM main_db where ID = '$dataid' ";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($result))
{
$name = $row[Name];
$type = $row[Type];
$year = $row[Year];
}
$rows = array('name' => $name, 'type' => $type, 'year' => $year);
echo json_encode($rows);
endif;
?>
我不确定是否有必要使用json。也许有其他更简单的方法来做到这一点。
答案 0 :(得分:1)
使用此代码:),希望这有帮助:)
$.ajax({ //Ajax call.
type: "GET",
url: "search.php",
data: 'id=' + idvalue ,
type: 'json',
success: function(msg){
// Show results in textareas.
msg = JSON.parse( msg ); // Line added
$('#resultname').val(msg.name);
$('#resultyear').val(msg.year);
$('#resulttype').val(msg.type);
}
}); // Ajax Call
添加行: msg = JSON.parse(msg); //添加行
当接收者形成url时这个msg是字符串,这不是json代码,你应该用函数 JSON.parse([string])解析为json 去解析 msg 从字符串到json代码:) ...
更新
$('#resultname').val(msg.name);
$('#resultyear').val(msg.year);
$('#resulttype').val(msg.type);
答案 1 :(得分:0)
确定。终于我发现了问题。它必须是.html
$( '#resultname')HTML(msg.name);
但首先它仍然无效。我花了5个小时阅读约。关于这一点的20篇文章,但都没有。我开始相信问题来自其他地方。并找到了它。
json之前的第一个字母必须是大写
输入:'json', - 不工作
输入:'json', - 工作
我不知道其他人是否喜欢这样敏感。即时通讯使用记事本++。这个小东西需要5个小时才能找到并导致问题。 无论如何,它现在正在工作,感谢Hoang的回答。