Ive just started to learn php a few weeks ago. I'm trying to do the same catalog program which i did in Access. But now im stuck. (i wrote a short copy of the program cos that is too long so this is similar as that) The problem is the next. I have an inputbox(ID) which would be an id number and some textareas(in this example 3 of them name-year-type) for the results. or it could be some other objects. When i enter the value the database gives me back the results from the fields where the id is the value i gave. Thats so far working with ajax but it gives to the 3 textareas the same results. What i would like to achieve is that all different field result be shown in different textareas. Ive tried with json but not really working. I will put up here the one with ajax (which is working good but the result should be seperated) and the one i tried with json. please if u have idea help me out.
This one is working but does not separate the results:
<!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;
?>
Here i tried with json. im sure that there are a lot of mistakes.
<!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;
?>
Im not sure if necessary to use json. Maybe there is other and simplier way to do it.