【问题标题】:get data from mysql with ajax and json into different textareas使用ajax和json从mysql获取数据到不同的文本区域
【发布时间】: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。也许还有其他更简单的方法可以做到这一点。

【问题讨论】:

标签: php mysql ajax json


【解决方案1】:

好的。最后我发现了问题。它必须是 .html

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

但首先它仍然不起作用。我花了大约 5 个小时阅读了大约。 20 篇关于此的文章,但没有一篇有效。我开始相信问题来自其他地方。并找到了。

json 前的类型首字母必须大写

type: 'json', -- 不工作

类型:'json',--工作

我不知道其他 progs 是否像这样敏感。我使用记事本++。这个 st**id 小东西花了 5 个小时才找到并导致了问题。 无论如何,它现在一次又一次地工作,感谢 Hoang 的回答。

【讨论】:

  • 不应该是dataType : 'json',
【解决方案2】:

使用此代码 :),希望对您有所帮助 :)

 $.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); // 添加的行

当receiver form url这个msg是字符串,这不是json代码,你应该用函数JSON.parse([string])解析成json去解析msg从字符串到 json 代码 :) ...

更新

$('#resultname').val(msg.name);
 $('#resultyear').val(msg.year);
 $('#resulttype').val(msg.type);

【讨论】:

  • 告诉 OP 为什么他们应该“使用此代码”。为什么不一样?为什么会有帮助?请务必使用这些信息编辑您的帖子。
  • 我不知道,我无法读懂别人的想法。也许他们想要更好地解释这将如何解决 OP 的问题。
猜你喜欢
  • 2015-04-03
  • 2018-07-07
  • 2016-09-22
  • 2014-05-07
  • 2021-08-02
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
相关资源
最近更新 更多