[英]Execute a Query/StoredProcedure that is stored as a field value in a database table with PHP and JQuery?
我要实现的目标有点复杂,因此我将尝试尽可能简单地进行解释。
这是一个有用的小提琴,目的只是为了让您了解单击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按钮在表叠加层中执行查询。 现在,我知道我需要为此使用JQuery,AJAX和/或JS。 我对他们不是很好,这就是我需要帮助的地方。 我的“覆盖”表中的存储过程从“电话”表中获取结果。
map.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']
的$_POST['sp_value']
变量中可用。 您可以像使用任何其他变量一样使用它。
正确设置好AJAX调用后,您将可以在浏览器的控制台中观看请求/响应。 这样,您可以验证AJAX请求正在发送正确的数据,然后从请求中接收回正确的数据。
再次说明:我不确定将代码放置在页面的总体布局中的位置,因此将您的代码放置在文档就绪处理程序中。 即使将jQuery代码放在</body>
标记之前,您也应该养成使用文档就绪处理程序的习惯。
您将在success : function(data)
函数中收到数据。 返回的信息将包含在data
,这些data
可在您的页面中用于多种用途,包括像#resultdiv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.