首页 > 解决方案 > 使用 php 从 db 获取值的 AJAX 获取方法问题

问题描述

我试图使用 GET 从我的数据库中获取一些值到我的 ajax 方法。代码对我来说似乎很好,但我不确定这里出了什么问题

从 db- 获取值的 ajax 方法

$("#form1").submit(function(){//comparing the total quoted hours with duration of time selected 

    ticket_id = $('#ticket_id').val();
    total_hours = $('#total_hours').val();
    // alert(ticket_id); // this shows up and comes up as undefined

    $.ajax({
        url: 'comp_time.php',
        type: 'GET',
        data: {ticket_id: ticket_id, total_hours: total_hours} ,
        success: function (response) {

            alert(total_hours) // get the total hours from comp_time.php 

        }
    }); 
});

comp_time.php-

<?php
    include("../includes/connect.php");
    $ticket_id = $_REQUEST['ticket_id'];
    $total_hours = $_REQUEST['total_hours'];

    $sql="SELECT total_hours FROM `cmp_ticket_repository` WHERE ticket_id = '$ticket_id'";
    $query_sql=mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die(mysqli_error($GLOBALS["___mysqli_ston"]));

    if( mysqli_num_rows($query_sql) > 0){
        $row_sql=mysqli_fetch_assoc($query_sql);

        echo $row_sql['total_hours'];
    }
    else{
        echo 'invalid';
    }
?>

我希望能够从数据库中获取这些值并将它们存储在 var 中,以便能够将其值与其他值进行比较。我在另一个使用 ajax get 的函数之后编写了这段代码,这是我的一个朋友编写的,并且几乎是以相同的方式编写的。

标签: phpjqueryajax

解决方案


从 db- 获取值的 ajax 方法

$("#form1").submit(function(){//comparing the total quoted hours with duration of time selected 

    ticket_id = $('#ticket_id').val();
    total_hours = $('#total_hours').val();
    // alert(ticket_id); // this shows up and comes up as undefined

    $.ajax({
        url: 'comp_time.php',
        type: 'GET',
        data: {ticket_id: ticket_id, total_hours: total_hours} ,
        success: function (response) {

            alert(response) // get the total hours from comp_time.php 

        }
    }); 
});

comp_time.php-

<?php
    include("../includes/connect.php");
    $ticket_id = $_REQUEST['ticket_id'];
    $total_hours = $_REQUEST['total_hours'];

    $sql="SELECT total_hours FROM `cmp_ticket_repository` WHERE ticket_id = '$ticket_id'";
    $query_sql=mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die(mysqli_error($GLOBALS["___mysqli_ston"]));

    if( mysqli_num_rows($query_sql) > 0){
        $row_sql=mysqli_fetch_assoc($query_sql);

        echo $row_sql['total_hours']; exit;
    }
    else{
        echo 'invalid'; exit;
    }
?>

推荐阅读