php - 使用 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 的函数之后编写了这段代码,这是我的一个朋友编写的,并且几乎是以相同的方式编写的。
解决方案
从 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;
}
?>
推荐阅读
- django - Django:Whitenoise 在调试错误的情况下无法在生产中工作
- javascript - XMLHttpRequest 替换元素不适用于 LocalStorage
- flutter - 在颤动的屏幕上显示来自服务器的 JSON 消息
- css - 向 shadowDOM 中动态创建的样式表添加规则
- logstash - Cloudfront 登录 Prometheus
- c# - (a,b) = (1,2) 是什么意思?
- python - 在 Python 中循环正则表达式,直到输入正确
- javascript - 限制js文件内容的范围
- javascript - 在 React 应用程序中更新动态呈现的元素的问题
- python - 在通过 Python 使用 Root 中创建带有分支的 TTree