gpt4 book ai didi

php - 使用 PHP 和 JQuery 执行作为字段值存储在数据库表中的查询/存储过程?

转载 作者:行者123 更新时间:2023-11-29 12:36:23 27 4
gpt4 key购买 nike

我想要实现的目标有点复杂,所以我会尝试尽可能简单地解释。

Here is a working fiddle只是为了让您了解我想展示的内容当您单击 DIV 时,它将切换另一个包含内部信息的 DIV(从我控制的数据库表中获取),这就是我想要显示来自电话的“时间”的地方表!

提前致谢,如果我需要澄清事情,请告诉我,因为我真的陷入困境!

我有下面两个表和服务器:

connection server 1: mario2001
table: overlays
+---------------------------------------------------------------------------------------------+
| q_id | button | query |
+---------------------------------------------------------------------------------------------+
| 12 | AHT_Stats | CALL telephony.sp_get_spec_stat_all_agents( '2014-11-02', '2014-11-02' ) |
+---------------------------------------------------------------------------------------------+

我对下面的服务器/表只有读取权限

connection server 2: mario2003
table: telephony
+-------------+
| memo | time |
+-------------+
| JOE | 410 |
+-------------+

问题:

我想知道是否可以(如果是如何?)通过单击带有 onClick 事件的 HTML 按钮来执行表 overlays 中的查询。现在,我知道我需要 JQuery、AJAX 和/或 JS。我不太擅长这些,这就是我需要帮助的地方。我的“覆盖”表中的存储过程正在从“电话”表获取结果。

ma​​p.php(此处显示的所有内容)获取我的 StoredProcedure 字段值并将其存储为变量:

    //Overlay table
$query_overlay_sql = "SELECT query FROM overlays";
$query_overlay_result = mysqli_query($dbh1,$query_overlay_sql);

//get the query from the "query" field in the overlay table
//and store it as a variable for later use for the AHT button
if($row = mysqli_fetch_assoc(($query_overlay_result))){
$sp_value = $row['query'];
}

HTML/JQuery:

 <!-- show the results in this DIV below-->
<div id="resultdiv" class="resultdiv" style="display:none">Time: <? /* get the time value*/ ?></div>

<script type="text/javascript">
$(document).ready(function() {
$('#aht').click(function(){
var sp_value_to_send = <?php echo $sp_value; ?>;
$.ajax({
type:"POST",
url : "show_aht.php",
data: { sp_value: sp_value_to_send }, // pass data here
success : function(data){
$('#resultdiv').show();
$('#resultdiv').html(data);
}//end success
})//end ajax
});//end click
});//end rdy
</script>

show_aht.php:

<?php
include 'db_conn_retca2003.php';


/****************************************************
Execute the query_value when AHT button is clicked
/****************************************************/
$dbh2_result = mysqli_query($dbh2, $query_value)
or die("Query fail: " . mysqli_error());

//loop the result set

while ($row2 = mysqli_fetch_assoc($dbh2_result)){
$memo = $row2['memo_code'];
$time = $row2['avg_handle_time'];
echo '<p>memo: '. $memo . ' time:' . $time . '</p><br>';
}
?>

最佳答案

您需要添加一个配置项来传递数据 -

<script type="text/javascript">
$(document).ready(function() {
$('#aht').click(function(){
var sp_value_to_send = <?php echo $foo; ?>;
$.ajax({
type:"POST",
url : "show_aht.php",
data: { sp_value: sp_value_to_send }, // pass data here
success : function(data){
$('#resultdiv').show();
$('#resultdiv').html(data);
}
});
});
});
</script>

该值现在可在 $_POST['sp_value'] 中找到show_aht.php 中的变量。您可以像使用任何其他变量一样使用它。

正确设置 AJAX 调用后,您将能够在浏览器的控制台中观看请求/响应。这样您就可以验证 AJAX 请求是否发送了正确的数据,并且您随后从请求中接收到了正确的数据。

另一个注意事项:我将您的代码放置在文档就绪处理程序中,因为我不确定您的代码放置在页面的总体布局中的位置。您应该养成使用文档就绪处理程序的习惯,即使您将 jQuery 代码放在结束 </body> 之前也是如此。标签。

您将在 success : function(data) 中收到返回的数据功能。返回的信息在 data它可以在您的页面中用于多种用途,包括像您使用 #resultdiv 所做的那样显示

关于php - 使用 PHP 和 JQuery 执行作为字段值存储在数据库表中的查询/存储过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26721613/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com