mysql 调用js函数_javascript – MySQL在JS函数中选择结果

我找到了一个简单的JS脚本作为聊天机器人.

在脚本本身中,lastUserMessage的结果是内联预定义的

if (lastUserMessage === 'name') {

botMessage = 'My name is ' + botName;

}

我想要实现的是如果JS在db中搜索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("" + botName + ": " + 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代码

需要做什么?

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("" + botName + ": " + 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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值