我正在用PHP和AIML做一个项目,机器人通过麦克风回答用户提出的用户查询。我是jQuery和JS的新手,所以我需要帮助在用户输入和最终输出之间实现加载效果。
<?php
$display = "";
$thisFile = __FILE__;
if (!file_exists('../../config/global_config.php'));
require_once ('../../config/global_config.php');
require_once ('../chatbot/conversation_start.php');
$get_vars = (!empty($_GET)) ? filter_input_array(INPUT_GET) : array();
$post_vars = (!empty($_POST)) ? filter_input_array(INPUT_POST) : array();
$form_vars = array_merge($post_vars, $get_vars); // POST overrides and overwrites GET
$bot_id = (!empty($form_vars['bot_id'])) ? $form_vars['bot_id'] : 1;
$say = (!empty($form_vars['say'])) ? $form_vars['say'] : '';
$convo_id = session_id();
$format = (!empty($form_vars['format'])) ? $form_vars['format'] : 'html';
?>
<!DOCTYPE html>
<html>
<head>
<title>Interact With Sia</title>
<script src="jquery.js"></script>
<script type="text/javascript" src="talk.js"></script>
</head>
<body onload='document.getElementById("say").focus(); document.getElementById("btn_say").style.display="none";'>
<center>
<h3>Talk to Sia</h3>
<form id="chatform1" name="chatform" method="post" action="index.php#end" >
<!-- <label for="say">Say:</label> -->
<input type="text" name="say" id="say" size="70" onmouseover="startDictation()" style="color:red" />
<input type="submit" class="say" name="submit" id="btn_say" value="say" />
<script>
$('#say').trigger('mouseover');
</script>
<input type="hidden" name="convo_id" id="convo_id" value="<?php echo $convo_id;?>" />
<input type="hidden" name="bot_id" id="bot_id" value="<?php echo $bot_id;?>" />
<input type="hidden" name="format" id="format" value="<?php echo $format;?>" />
</form>
<br/><br/>
<?php echo $display; ?> //THIS DISPLAYS THE OUTPUT TO THE QUERY
</center>
</body>
</html>
页面一加载,用户浏览器中的麦克风就会被激活[通过talk.js_startDiction()],用户完成语音输入后,查询就会发送到脚本,然后<?php echo $display; ?>
会显示结果。
在脚本返回查询结果并更新页面上的$display
变量之前,我如何实现加载效果来代替<?php echo $display; ?>
?
您可以使用AJAX
对此进行归档,例如:
1-加载jquery.js
(您可以使用CDN或下载它)。
2-用加载程序创建一个div(通常gif很好,有很多)。用css隐藏:display:hidden;
3-在页面底部添加:
<script>
// Attach a submit handler to the form
$( "#chatform1" ).submit(function( event ) {
// Start the loader
$('#loader').show();
// Stop form from submitting normally
event.preventDefault();
// Get some values from elements on the page:
var data = $(this).serialize();
var url = "urlToYourPhpFile.php";
// Send the data using post
var posting = $.post( url, { data: data } );
// Put the results in a div
posting.done(function( dataResponse ) {
//do something with dataResponse
$('#loader').hide();
});
});
</script>
当表单被提交时,jquery"捕获"请求,处理.submit()
函数,并通过post将数据发送到.php
文件(您应该创建它),并使用$_POST
接收参数。类似于:
yourPhpFile.php
<?php
$convo_id = $data['convo_id'];
//do something
...
?>