gpt4 book ai didi

javascript - JS函数中的MySQL选择结果

转载 作者:太空狗 更新时间:2023-10-29 13:42:29 25 4
gpt4 key购买 nike

我找到了一个用作聊天机器人的简单 JS 脚本。在脚本本身中,lastUserMessage 的结果是内联预定义的,例如

if (lastUserMessage === 'name') {
botMessage = 'My name is ' + botName;
}

我想要实现的是,如果 JS 在数据库中搜索 lastUserMessage 并从那里提供 botMessage

我确信它不应该那么难,但我不知道该怎么做。

这是JS代码:

nlp = window.nlp_compromise;
var messages = [], //array that hold the record of each string in chat
lastUserMessage = "", //keeps track of the most recent input string from the user
botMessage = "", //var keeps track of what the chatbot is going to say
botName = 'Bot Name', //name of the chatbot
talking = true; //when false the speach function doesn't work

//edit this function to change what the chatbot says
function chatbotResponse() {
talking = true;
botMessage = "Ops... didn't get this"; //the default message

if (lastUserMessage === 'name') {
botMessage = 'My name is ' + botName;
}

}

//this runs each time enter is pressed.
//It controls the overall input and output
function newEntry() {
//if the message from the user isn't empty then run
if (document.getElementById("chatbox").value != "") {
//pulls the value from the chatbox ands sets it to lastUserMessage
lastUserMessage = document.getElementById("chatbox").value;
//sets the chat box to be clear
document.getElementById("chatbox").value = "";
//adds the value of the chatbox to the array messages
messages.push(lastUserMessage);
//Speech(lastUserMessage); //says what the user typed outloud
//sets the variable botMessage in response to lastUserMessage
chatbotResponse();
//add the chatbot's name and message to the array messages
messages.push("<b>" + botName + ":</b> " + botMessage);
// says the message using the text to speech function written below
Speech(botMessage);
//outputs the last few array elements of messages to html
for (var i = 1; i < 8; i++) {
if (messages[messages.length - i])
document.getElementById("chatlog" + i).innerHTML = messages[messages.length - i];
}
}
}

//runs the keypress() function when a key is pressed
document.onkeypress = keyPress;
//if the key pressed is 'enter' runs the function newEntry()
function keyPress(e) {
var x = e || window.event;
var key = (x.keyCode || x.which);
if (key == 13 || key == 3) {
//runs this function when enter is pressed
newEntry();
}
if (key == 38) {
console.log('hi')
//document.getElementById("chatbox").value = lastUserMessage;
}
}

//clears the placeholder text ion the chatbox
//this function is set to run when the users brings focus to the chatbox, by clicking on it
function placeHolder() {
document.getElementById("chatbox").placeholder = "";
}

这是HTML代码

<div id='bodybox'>
<div id='chatborder'>
<p id="chatlog2" class="chatlog">&nbsp;</p>
<p id="chatlog1" class="chatlog">&nbsp;</p>
<input type="text" name="chat" id="chatbox" placeholder="Hi there! Type here to talk to me." onfocus="placeHolder()">
</div>

需要发生什么?

Ideally, the script should take the values "lastUserMessage" and "botMessage" from a db that has 2 columns "lastUserMessage" and "botMessage".

我试图做的是按照下面 Ghost 的评论...但没有奏效。

    nlp = window.nlp_compromise;
var messages = [], //array that hold the record of each string in chat
lastUserMessage = "", //keeps track of the most recent input string from the user
botMessage = "", //var keeps track of what the chatbot is going to say
botName = 'Bot Name', //name of the chatbot
talking = true; //when false the speach function doesn't work

//edit this function to change what the chatbot says
function chatbotResponse() {
talking = true;
botMessage = "Ops... didn't get this"; //the default message

$.ajax({
url: 'db_query.php',
data: "lastUserMessag=lastUserMessag",
dataType: 'json',
success: function(data)
{
var lastUserMessage_db = data[0];
var botMessage_db= data[1];

if (lastUserMessage === lastUserMessage_db) {
botMessage = botMessage_db;
}
}
});

}

//this runs each time enter is pressed.
//It controls the overall input and output
function newEntry() {
//if the message from the user isn't empty then run
if (document.getElementById("chatbox").value != "") {
//pulls the value from the chatbox ands sets it to lastUserMessage
lastUserMessage = document.getElementById("chatbox").value;
//sets the chat box to be clear
document.getElementById("chatbox").value = "";
//adds the value of the chatbox to the array messages
messages.push(lastUserMessage);
//Speech(lastUserMessage); //says what the user typed outloud
//sets the variable botMessage in response to lastUserMessage
chatbotResponse();
//add the chatbot's name and message to the array messages
messages.push("<b>" + botName + ":</b> " + botMessage);
// says the message using the text to speech function written below
Speech(botMessage);
//outputs the last few array elements of messages to html
for (var i = 1; i < 8; i++) {
if (messages[messages.length - i])
document.getElementById("chatlog" + i).innerHTML = messages[messages.length - i];
}
}
}

//runs the keypress() function when a key is pressed
document.onkeypress = keyPress;
//if the key pressed is 'enter' runs the function newEntry()
function keyPress(e) {
var x = e || window.event;
var key = (x.keyCode || x.which);
if (key == 13 || key == 3) {
//runs this function when enter is pressed
newEntry();
}
if (key == 38) {
console.log('hi')
//document.getElementById("chatbox").value = lastUserMessage;
}
}

//clears the placeholder text ion the chatbox
//this function is set to run when the users brings focus to the chatbox, by clicking on it
function placeHolder() {
document.getElementById("chatbox").placeholder = "";
}

在 DB_query.php 中我有

$p = $_GET['lastUserMessag']; 
$query=mysql_query("SELECT lastUserMessag, botMessage FROM `aiml` WHERE lastUserMessag='$p'");
$array = mysql_fetch_row($query);
echo json_encode($array);

最佳答案

您正在执行的 Javascript 当前在浏览器中运行,但未连接到您可以从中获取数据的任何数据库。

为此,您必须向您的后端服务器(假设您有一个)发送一个 POST 或 GET 请求,我认为该请求是使用 NodeJS 用 JS 编写的。

请注意,您应该绝不 授予用户对所述数据库的访问权限(例如,将数据库连接到您现在正在使用的代码,该代码在浏览器中执行)然后他们就可以用它做任何他们想做的事。

如果你想查看NodeJS以及如何处理请求,我建议你看看ExpressJSthis great MySQL + NodeJS tutorial在 W3School 上取决于您的专业水平

关于javascript - JS函数中的MySQL选择结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47288054/

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