1

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.

my form

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.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
bontoo
  • 137
  • 1
  • 19

2 Answers2

1

Use this code :), hope this help :)

 $.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

added line: msg = JSON.parse( msg ); // Line added

when receiver form url this msg is string, this not a json code, You should parse to json with function JSON.parse([string]) go parse msg from string to json code :) ...

update

$('#resultname').val(msg.name);
 $('#resultyear').val(msg.year);
 $('#resulttype').val(msg.type);
HoangHieu
  • 2,802
  • 3
  • 28
  • 44
  • 3
    Tell the OP why they should "use this code". Why is it different? Why will it help? Make sure to edit your post with the information. – Jay Blanchard Oct 16 '14 at 18:10
  • I don't know, I cannot read other people's minds. Perhaps they wanted a better explanation of how this will fix the OP's problem. – Jay Blanchard Oct 16 '14 at 18:17
0

ok. finally i found the problem. it has to be .html

$('#resultname').html(msg.name);

but first it still did not work. i spent like 5 hours and read approx. 20 article about this and none of that worked. And i started to believe that the problem comes from somewhere else. And found it.

the first letter of type before the json has to be uppercase

type: 'json', -- not working

Type: 'json', -- working

I dont know if other progs sensitive like this or not. im using notepad++. this st**id small things took 5 hours to be found and caused the problem. Anyway it is working now and again thanks for Hoang for the answer.

bontoo
  • 137
  • 1
  • 19