Ive几周前才开始学习php。我正在尝试做与Access中相同的目录程序。但是现在我卡住了。(我写了一份程序cos的简短副本,该副本太长了,所以这与之相似)问题是下一个。我有一个inputbox(ID),它是一个ID号和一些textareas(在此示例中为3个name-year-type)作为结果。或者可能是其他一些对象。当我输入值时,数据库会从ID值为我给出的值的字段中返回结果。到现在为止,使用ajax都可以,但是它给3个textareas带来了相同的结果。我想实现的是,所有不同的字段结果都显示在不同的文本区域中。香港专业教育学院尝试与JSON,但不是真正的工作。我将在这里放置一个使用ajax(效果很好,但结果应分开显示)和我尝试使用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>
</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。也许还有其他更简单的方法可以做到这一点。
使用此代码:),希望有帮助:)
$.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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句