T
T
TechNOIR2018-02-28 11:33:08
PHP
TechNOIR, 2018-02-28 11:33:08

Jquery. How to fix the problem with constant execution of received Javascript from Php?

Good afternoon.

I agree, the title of the question can be confusing, but I'll try to explain))

I have this js+jquery script

var username = "";
var user = "Пользователь"
var botname = "Бот";

function send_message(conv,message){
    if (conv.length > 4) {
            conv = conv + "<br>";
    }
    $("#converse").html(conv +"<script></script><div class='message-box left-img'><div class='picture'><img src='img/a.png' title='user name'></div><div class='mes'><span class = 'current-msg'>" + "<span id='chat-bot'>" + botname + ": </span><p>" + message + "</p></span></div></div>");
    $(".current-msg").hide();
    $(".current-msg").delay(500).fadeIn(function() {
        $(".current-msg").removeClass("current-msg");
    var e = document.getElementById('converse');
        e.scrollTop=e.scrollHeight
    })
    }
function get_username(conv){
    send_message(conv,"Привет, могу ли я чем то помочь?");
}

function ai(conv,message){
        // $("#send").click(function(){
            $.get("get.php", {q:message}, function(data, status){
                // alert("Data: " + data + "\nStatus: " + status);
                send_message(conv,data);
                });
            console.log('Я всё');
            
        // });
}
$(function(){
    var open = false;
    var conv = $("#converse").html();
    get_username(conv);
    $("#send").click(function(){
        var usermsg = $("#textbox").val();
        conv = $("#converse").html();
        console.log(conv.length);
    if (usermsg != "") {
      $("#textbox").val("");
      if (conv.length > 4) {
        conv = conv + "<br>";
      }
      $("#converse").html(conv + "<div class='message-box right-img'><div class='picture'><img src='img/user.png' title='user name'></div><div class='mes'><span><span id='chat-user'>" + user + ": </span><p>" + usermsg + "</p></span></div></div>");
      $("#converse").scrollTop($("#converse").prop("scrollHeight"));
      conv = $("#converse").html();
      ai(conv,usermsg);
      }
  });
  $("#chat-button").click(function(){
    $("#chat-box").animate({"right":"0px"});
  });
  $("#cancel").click(function(){
    $("#chat-box").animate({"right":"-300px"});
  });
});


document.forms.sendtext.addEventListener("submit", function (e) {
  e.preventDefault();

  var text = this.elements.text.value;
   $("#send").click();
});

document.forms.onkeyup = function(event) { // слушаете событие onkeyup  формы
  if (event.keyCode == 13) { //  коли enter - дёргаете вашу штатную функцию отправки
    buttonOtprav2();
  }
};

function buttonOtprav2() {
  console.log('enter is firing');
  event.preventDefault();
}



Meaning:
We enter a request in the input, jquery requests /get.php?q=Query and returns the result to the list of messages.
In get php, the list of available commands and each command corresponds to the execution of a script, for example:
Привет => include '/script/privet.php'
Пока => include '/script/poka.php'


Everything worked fine until I started injecting Javascript (and html code in general) into privet.php and poka.php
Well, for example, privet.php
<?php
блаблабла
?>
<script>
alert('бла бла бла')
</script>


For direct requests through the browser /get.php?q=Hi and /get.php?q=So far everything works fine. Greetings, alert is working, but for now, it is working out its script
BUT!
When we write Hello in the input (/get.php?q=Hi is returned via jquery) it also works, BUT when the next "Bye" command is called, the script from the previous request continues to be processed.
And so for any requests in the input UNTIL THE PAGE IS RELOADED

Any ideas guys? ... I've already broken my whole head)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sesil_B, 2018-03-01
@Sesil_B

I have ideas - understand how javascript works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question