У меня небольшая проблема с моим простым чат-ботом, он задает ряд вопросов и ответов с использованием PHP и MySQL, но главным фактором здесь является код Javascript, который показан ниже
// elements used in the chat system //
var sendbutton = document.getElementById('sendbutton');
var userinput = document.getElementById('textbox');
var messagecontainer = document.getElementById('messagescontainer');
var httprequest = new XMLHttpRequest(); // this is for the connection with the php file for responses//
var units;var price;var space; var down;
//---------------------------------//
sendbutton.addEventListener('click',function(e){
//check if the message is empty or not//
if(userinput.value == ""){
alert("Please type in a message");
}else{
window.location = "url.php";
let messagetext = userinput.value; // get the message from the user//
//-- conditions to store user requirements --//
if(messagetext === "Primary" || messagetext === "Resale"){
units = messagetext;
}
if(messagetext == "5" || messagetext == "10" || messagetext == "15"){
down = messagetext;
}
if(messagetext == "5 Million to 10 Million" || messagetext == "10 Million to 30 Million"){
price = messagetext;
}
if(messagetext == "100 - 150" || messagetext == "150 - 250" || messagetext == "250+"){
space = messagetext;
window.open("test2.php?units="+units+"&&price="+price+"&down="+down+"&&space="+space+"");
}
//-------------------------------------------//
SendMessage(messagetext); // send the message to the function//
userinput.value=""; // to remove the text message from the input box after the user presses send//
}
} );
//-------------------------------//
// the action that will happen when the user presses send //
function SendMessage(messagetext){
// create the message response from the user//
var UserMessage = document.createElement('div');
UserMessage.classList.add('w-50');
UserMessage.classList.add('float-end');
UserMessage.classList.add('shadow-sm');
UserMessage.style.margin="10px";
UserMessage.style.padding="5px";
UserMessage.style.backgroundColor="#0dcaf0";
UserMessage.innerHTML="Visitor: "+
""+ messagetext +"";
messagecontainer.appendChild(UserMessage);
//-----------------------------------------//
//---now we send the message of the user to the database using php//
AskDatabase(messagetext);
//scroll to the last message //
messagecontainer.scrollTop = messagecontainer.scrollHeight;
//---------------------------------------------------------------//
}
//-------------------------------------------------//
//--below is the function to ask the database about the user's questions//
function AskDatabase(messagetext){
//ajax used below//
httprequest.open('GET','chatbot.php?message='+messagetext,true);// get the php connection file//
httprequest.send();// send the user message to this php file//
httprequest.onreadystatechange = ChatBotMessage; //send the chat bot response to the user//
}
//----------------------------------------------------------------------//
//the message sent from the chatbot itself//
function ChatBotMessage(){
//check if there is a response from the database//
if(httprequest.readyState == XMLHttpRequest.DONE && httprequest.status == 200){
var BotFeedback = JSON.parse(httprequest.responseText);
// create the message response from the bot//
var BotMessage = document.createElement('div');
BotMessage.classList.add('w-50');
BotMessage.classList.add('float-start');
BotMessage.classList.add('shadow-sm');
BotMessage.style.margin="10px";
BotMessage.style.padding="5px";
BotMessage.style.backgroundColor="#ced4da";
BotMessage.style.height="200px";
BotMessage.innerHTML="Rebot: "+
""+ BotFeedback+"";
messagecontainer.appendChild(BotMessage);
//-----------------------------------------//
//scroll to the last message //
messagecontainer.scrollTop = messagecontainer.scrollHeight;
//---------------------------//
}
//--------------------------------------------//
}
//----------------------------------------//
//-- the below is to automatically select for the user in his input the type of request he wants//
function myFunction(e) {
document.getElementById("textbox").value = e.target.value; // this is the id of the textbox input
}
//-------------------------------------------------------------------------------------------//
Основная проблема в том, что строка:
window.open("test2.php?units="+units+"&&price="+price+"&down="+down+"&&space="+space+"");
не перенаправляется на другую страницу, несмотря на сохранение значений.
Приведенный ниже PHP-код предназначен для справки:
Подробнее здесь: https://stackoverflow.com/questions/793 ... a-new-page
Javascript не перенаправляет на новую страницу ⇐ MySql
Форум по Mysql
-
Anonymous
1736349472
Anonymous
У меня небольшая проблема с моим простым чат-ботом, он задает ряд вопросов и ответов с использованием PHP и MySQL, но главным фактором здесь является код Javascript, который показан ниже
// elements used in the chat system //
var sendbutton = document.getElementById('sendbutton');
var userinput = document.getElementById('textbox');
var messagecontainer = document.getElementById('messagescontainer');
var httprequest = new XMLHttpRequest(); // this is for the connection with the php file for responses//
var units;var price;var space; var down;
//---------------------------------//
sendbutton.addEventListener('click',function(e){
//check if the message is empty or not//
if(userinput.value == ""){
alert("Please type in a message");
}else{
window.location = "url.php";
let messagetext = userinput.value; // get the message from the user//
//-- conditions to store user requirements --//
if(messagetext === "Primary" || messagetext === "Resale"){
units = messagetext;
}
if(messagetext == "5" || messagetext == "10" || messagetext == "15"){
down = messagetext;
}
if(messagetext == "5 Million to 10 Million" || messagetext == "10 Million to 30 Million"){
price = messagetext;
}
if(messagetext == "100 - 150" || messagetext == "150 - 250" || messagetext == "250+"){
space = messagetext;
window.open("test2.php?units="+units+"&&price="+price+"&down="+down+"&&space="+space+"");
}
//-------------------------------------------//
SendMessage(messagetext); // send the message to the function//
userinput.value=""; // to remove the text message from the input box after the user presses send//
}
} );
//-------------------------------//
// the action that will happen when the user presses send //
function SendMessage(messagetext){
// create the message response from the user//
var UserMessage = document.createElement('div');
UserMessage.classList.add('w-50');
UserMessage.classList.add('float-end');
UserMessage.classList.add('shadow-sm');
UserMessage.style.margin="10px";
UserMessage.style.padding="5px";
UserMessage.style.backgroundColor="#0dcaf0";
UserMessage.innerHTML="Visitor: "+
""+ messagetext +"";
messagecontainer.appendChild(UserMessage);
//-----------------------------------------//
//---now we send the message of the user to the database using php//
AskDatabase(messagetext);
//scroll to the last message //
messagecontainer.scrollTop = messagecontainer.scrollHeight;
//---------------------------------------------------------------//
}
//-------------------------------------------------//
//--below is the function to ask the database about the user's questions//
function AskDatabase(messagetext){
//ajax used below//
httprequest.open('GET','chatbot.php?message='+messagetext,true);// get the php connection file//
httprequest.send();// send the user message to this php file//
httprequest.onreadystatechange = ChatBotMessage; //send the chat bot response to the user//
}
//----------------------------------------------------------------------//
//the message sent from the chatbot itself//
function ChatBotMessage(){
//check if there is a response from the database//
if(httprequest.readyState == XMLHttpRequest.DONE && httprequest.status == 200){
var BotFeedback = JSON.parse(httprequest.responseText);
// create the message response from the bot//
var BotMessage = document.createElement('div');
BotMessage.classList.add('w-50');
BotMessage.classList.add('float-start');
BotMessage.classList.add('shadow-sm');
BotMessage.style.margin="10px";
BotMessage.style.padding="5px";
BotMessage.style.backgroundColor="#ced4da";
BotMessage.style.height="200px";
BotMessage.innerHTML="Rebot: "+
""+ BotFeedback+"";
messagecontainer.appendChild(BotMessage);
//-----------------------------------------//
//scroll to the last message //
messagecontainer.scrollTop = messagecontainer.scrollHeight;
//---------------------------//
}
//--------------------------------------------//
}
//----------------------------------------//
//-- the below is to automatically select for the user in his input the type of request he wants//
function myFunction(e) {
document.getElementById("textbox").value = e.target.value; // this is the id of the textbox input
}
//-------------------------------------------------------------------------------------------//
Основная проблема в том, что строка:
window.open("test2.php?units="+units+"&&price="+price+"&down="+down+"&&space="+space+"");
не перенаправляется на другую страницу, несмотря на сохранение значений.
Приведенный ниже PHP-код предназначен для справки:
Подробнее здесь: [url]https://stackoverflow.com/questions/79339716/javascript-does-not-redirect-to-a-new-page[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия