【发布时间】:2014-12-12 05:06:44
【问题描述】:
我几周前才开始学习 php。我正在尝试执行我在 Access 中执行的相同目录程序。 但现在我卡住了。 (我写了一个太长的程序cos的简短副本,所以这和那个相似) 问题是下一个。 我有一个输入框(ID),它是一个 ID 号和一些文本区域(在这个例子中,其中 3 个是名称-年份类型)作为结果。或者它可能是其他一些对象。 当我输入值时,数据库会从 id 是我给出的值的字段中返回结果。 到目前为止,这就是使用 ajax,但它为 3 个文本区域提供了相同的结果。 我想要实现的是所有不同的字段结果都显示在不同的文本区域中。我尝试过使用 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。也许还有其他更简单的方法可以做到这一点。
【问题讨论】: