无法使用onClick事件从数据库中检索数据

时间:2016-06-16 16:35:26

标签: javascript php jquery html json

我一直在为我的州考试做一个学校项目,我在使用php文件从数据库中获取数据时遇到了一些问题。

HTML:

<!DOCTYPE html>
<html class="no-js" lang="en-US">
    <head>
        <title>Programma Scolastico</title>
        <link rel="icon" href="Immagini/icon.png">
        <link rel="stylesheet" href="Css/CssSitoWeb.css">
        <link rel="stylesheet" href="Css/animate.min.css">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <script src="https/ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        <script src="Js/jquery.lettering.js"></script>
        <script src="Js/jquery.textillate.js"></script>
        <script src="Js/app.js"></script>

        <div id="background">
            <div id="in-back">
                <span class="title">Programma 5AIA</span>
                </br><button id="column-opener">Vade</button>
            </div>
        </div>

        <div id="paragraph-column">
        </div>
    </body>
</html>

这里提供问题的php文件(未定义索引:idMateria - 第5行)

ElencoArgomentiByMaterie.php:

<?php
    include 'CredenzialiAccesso.php';
    $conn = new PDO ($str, $user, $pwd);
    $stmt = $conn -> prepare("SELECT * FROM Argomento WHERE Materia_idMateria = :idMat")
    $stmt -> bindValue(':idMat', $_GET["idMateria"]);
    if ($stmt -> execute())
    {
        echo json_encode($stmt -> fetchAll());
    }
?>

这里是创建用于触发onClick事件的按钮的函数:

$( document ).ready(function()
{
    $(function ()
    {
        $('.title').textillate(
        {
            in: {effect: 'rollIn'}
        });
    })
$("#column-opener").click(function()
    {
        $("#background").animate({left: '272px'});
        $("#in-back").animate({right: '125px'});
        $("#column-opener").hide();

        $.getJSON( "Php/ElencoMaterie.php", function(res)
        {
            var form = $('<form></form>');
            $.each(res, function(k, v)
            {
                var input = $('<input type="button" onclick="ArgomentoByMateria('+v['idMateria']+')" value="'+ v['nomeMateria'] +'" id="buttonMateria"/>');
                //var input = $('<input type="button" value="' + v['nomeMateria'] + '" id="buttonMateria" />');
                form.append(input);
            });
            $("#paragraph-column").empty().append(form);
        });
    });
});

这是点击时触发的功能:

function ArgomentoByMateria(idMat)
{
    $.getJSON("Php/ElencoArgomentiByMaterie.php", {'idMateria':idMat}, function(res)
    {
        $("#in-back").animate({right: '50px'});
        $("#in-back").css("margin", "130px 100px 50px 250px");
        $("#in-back").animate({top: '30px'});
        //$("#in-back").css("margin-top" , "50px");
        $("span").css("font-size" , "50px");

        if (res.length > 0)
        {
            $.each(res, function(k, v)
            {
                var row = $('<span>'+v['descrizione']+'</span> <button onclick="SottoArgomentoByArgomento('+v['idArgomento']+')" value="+"/></ br>');
                //var row = $('<span>'+v['descrizione']+'</span></ br>');
            });
            $("#in-back").append(row);
        }
    });
}

ElencoMaterie.php:

<?php
    include 'CredenzialiAccesso.php';
    $conn = new PDO ($str, $user, $pwd);
    $stmt = $conn -> prepare("SELECT nomeMateria FROM Materia");
    if ($stmt -> execute())
    {
        echo json_encode($stmt -> fetchAll());
    }
?>

PS:抱歉英语不好。

0 个答案:

没有答案