Home display data js from datebase sql
Reply: 1

display data js from datebase sql

KompanierosB
1#
KompanierosB Published in 2017-12-08 00:41:13Z

This question already has an answer here:

  • How do I return the response from an asynchronous call? 25 answers

I would like to push my value of textbox to sql and then display it. I read a lot topics but still nothing. I have always problem to explain my problems but I hope u will see what i want to do escpecialy when u look at db2.php

    $("#send").click(function(){

    var username = "<span class ='username' = >Klient: </span>";
    var newMessage = $("#textbox").val();
    nw = newMessage;
    $.ajax({
    type: "POST",
    url: "db2.php",
    data: {'name': nw },
    success: function (json) {
        jss = json.PYT;
        oss = json.ODP;
        console.log(jss);
    }

});
    $("#textbox").val("");

    var prevState = $("#container").html();


    if( prevState.length > 3){
        prevState = prevState + "<br>";
        }

        $("#container").html(prevState + username + newMessage);

        $("#container").scrollTop($("#container").prop("scrollHeight"));

        ai(newMessage);

    });

and my db2.php .

<?php
header('Content-type: application/json');
include 'connect.php';  

if (isset($_POST['name'])) {

$name = $_POST['name'];
$queryResult = $connect->query("select * from Chatbot where '$name' LIKE 
CONCAT('%',PYT,'%')");
$result = array();
while($pomoc = $queryResult->fetch_assoc()){
    $result[] = $pomoc;
}
}

echo json_encode($result);

Now my result is {}, echo is null.

console.log(nw) VM289:1 dsasa undefined I know how to get just output from ajax but if i want to push this data everything goes wrong. Best regards

UPDATE. Now I would like to get jss value out of this function to the other one.

   var jss = {}; //(first line code)

    $("#send").click(function(){

    var username = "<span class ='username' = >Klient: </span>";
    var newMessage = $("#textbox").val();
    nw = newMessage;
$.ajax({
type: 'POST',
url: 'db2.php',
data: { 
    'name': nw, 
},
success: function(data){
   jss = data[0].PYT;

}
});

UPDATE 2

var jss2 = {};
var nw;




$(function(){

username();

$("#textbox").keypress(function(event){
if ( event.which == 13) {
    if ( $("#enter").prop("checked") ){

        $("#send").click();
        event.preventDefault();
        }
    }
});
$("#send").click(function(){

    var username = "<span class ='username' = >Klient: </span>";
    var newMessage = $("#textbox").val();


    $("#textbox").val("");

    var prevState = $("#container").html();


    if( prevState.length > 3){
        prevState = prevState + "<br>";
        }

        $("#container").html(prevState + username + newMessage);

        $("#container").scrollTop($("#container").prop("scrollHeight"));

        ai(newMessage);

    });


})

function send_message(message){

    var prevState = $("#container").html();

    if(prevState.length > 3){
    prevState = prevState + "<br>";
    }

    $("#container").html(prevState + "<span class = 'bot'>Chatbot: </span>" + message);
}
function username(){
    $("#container").html("<span class = 'bot'>Chatbot: </span>Hi!");
}
function myFunction() {
var x = document.getElementById("textbox").value;

}

function ai(message){
    var jss;
    message = message.toLowerCase();
    nw = message;

    $.ajax({
type: 'POST',
url: 'db2.php',
data: { 
    'name': nw, 
},
success: function(data){
   jss = data[0].PYT;

}
});

console.log(jss);
    if ((message.indexOf(jss)>=0) || (message.indexOf("?")>=0)){
    send_message(Answer);
    return;
    }

    else{
    send_message("Nope ");
    }

}  
ADyson
2#
ADyson Reply to 2017-12-08 15:22:16Z

I think this is what you need to do with your function so that you can use the jss variable properly, once the ajax request has completed:

function ai(message){
  var jss;
  message = message.toLowerCase();
  nw = message;

  $.ajax({
    type: 'POST',
    url: 'db2.php',
    data: { 
      'name': nw, 
    },
    success: function(data){
      jss = data[0].PYT;
      console.log(jss);
      if ((message.indexOf(jss)>=0) || (message.indexOf("?")>=0)){
        send_message(Answer);
        return;
      }
      else{
        send_message("Nope ");
      }
    }
  });
}

Any code which relies on the jss variable must not be executed until after the ajax call has completed. Since ajax calls run asynchronously, the only way to guarantee this is for that code to be included in (or triggered from) the "success" callback function in your ajax request.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.384719 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO