使用PHP和JQuery执行一个查询/存储过程,该过程作为字段值存储在数据库表中?

使用PHP和JQuery执行一个查询/存储过程,该过程作为字段值存储在数据库表中?,php,jquery,mysql,ajax,mysqli,Php,Jquery,Mysql,Ajax,Mysqli,我想要达到的目标有点复杂,所以我会尽可能简单地解释 只是想让你知道我想展示什么 当您单击DIV时,它将切换另一个DIV,其中包含信息(从我控制的数据库表中获取),这就是我要从电话表中显示“时间”的地方 提前谢谢,如果我需要澄清,请让我知道,因为我真的卡住了 我有以下两个表和服务器: connection server 1: mario2001 table: overlays +------------------------------------------------------------

我想要达到的目标有点复杂,所以我会尽可能简单地解释

只是想让你知道我想展示什么 当您单击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  |
+-------------+
问题:

    //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']; 
    }
 <!-- 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>
<?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>';
  }
?>
我想知道,通过单击带有onClick事件的HTML按钮,是否有可能(如果是如何?)在表overlays中执行查询。现在,我知道我需要JQuery、AJAX和/或JS。我不太擅长,这就是我需要帮助的地方。我的“overlays”表中的存储过程正在从“telephony”表中获取结果

map.php(此处显示的所有内容)获取my StoredProcess字段值并将其存储为变量:

    //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']; 
    }
 <!-- 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>
<?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>';
  }
?>
HTML/JQuery:

    //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']; 
    }
 <!-- 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>
<?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>';
  }
?>

时间:
$(文档).ready(函数(){
$('#aht')。单击(函数(){
var sp_value_to_send=;
$.ajax({
类型:“POST”,
url:“show_aht.php”,
数据:{sp_value:sp_value_to_send},//在此处传递数据
成功:功能(数据){
$('#resultdiv').show();
$('#resultdiv').html(数据);
}//最终成功
})//结束ajax
});//结束单击
});//结束
show\u aht.php:

    //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']; 
    }
 <!-- 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>
<?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>

$(文档).ready(函数(){
$('#aht')。单击(函数(){
var sp_value_to_send=;
$.ajax({
类型:“POST”,
url:“show_aht.php”,
数据:{sp_value:sp_value_to_send},//在此处传递数据
成功:功能(数据){
$('#resultdiv').show();
$('#resultdiv').html(数据);
}
});
});
});
该值现在将在show\u aht.php中的
$\u POST['sp\u value']
变量中可用。您可以像使用任何其他变量一样使用它

一旦AJAX调用设置正确,您将能够在浏览器控制台中查看请求/响应。通过这种方式,您可以验证AJAX请求是否发送了正确的数据,然后您是否从请求返回了正确的数据

还有一个注意事项:我将您的代码放在文档就绪处理程序中,因为我不确定您的代码在页面的总体布局中的位置。您应该养成使用文档就绪处理程序的习惯,即使您将jQuery代码放在关闭标记之前


您将在
success:function(data)
函数中收到数据。返回的信息将保存在
数据中
,这些数据可以在您的页面中用于多种用途,包括使用
#resultdiv

进行显示。因此,尝试AJAX对您不起作用?@JayBlanchard我更新了我的代码,但仍然卡住了,不确定如何将我的sp_值传递到其他页面,它将显示查询结果谢谢!我已经准备好了文档。我只是没有包括它。我在控制台中看到一个错误,它说预期的“;”在呼叫和sp_获取_规范_统计_所有代理之间('2014-11-02','2014-11-02');这是要发送的sp_value_的值。另外,如何使用发送的数据在show#aht.php上执行查询,并将结果发送回我的#aht DIV中显示?不太熟悉AJAX。再次感谢,我更新了上面的JS代码,编辑了我的答案@mario。如果你还有其他问题,请告诉我。好的,现在我明白了。尽管如此,我还是无法在控制台中消除该错误消息,因为我的脚本没有运行,只是因为JS。上面写着SCRIPT1004:应该是“;”像这样:调用“;”sp_blablaI发现错误。我在sp_value_to_send上加了双引号,结果成功了