M
M
Miroslavux2022-04-12 14:43:50
MySQL
Miroslavux, 2022-04-12 14:43:50

How can I execute a new request to mysql on node js after a click?

Such a task, the user selects 1 of the three tables (table1, 2,3) and enters text into the field for which it is necessary to check for their presence in 2 fields of the table. And display up to 20 names and the total number of records by coincidence, sort everything alphabetically. If I enter hard data into the script, everything works with a bang, but here's how to make it so that after a click a new request is created on node js and the actual data is displayed.
PS I can write in php, but I started with current nodes.
PPS please do not judge strictly.
here is my code:

var mysql = require('mysql');
var http = require('http');
var table='table1';
var tex='';

 
/**
 * @type {Pool}
 */
var db = null;
 
http.createServer(router).listen(8080);
console.log('Server running on port 8080.');
//------------------------------------------------//
/**
 * @return {Pool}
 */
function getConncetion() {
    if (db === null) {
        db = mysql.createPool(getConfig())
    }
    return db;
}
/**
 * @returns {Object}
 */
function getConfig() {
    return {
        limit: 10,
        host: 'localhost',
        user: 'root',
        password: '123456',
        database: 'mydb'
    };
}
function router(req, res) {

    getConncetion().query('Select * from '+table+' where name like "%'+tex+'%" or description like "%'+tex+'%" ORDER BY name;', function (error, fields, result) {
        if (error) {
            throw error;
}
        res.writeHead(200, {'Content-Type': 'text/html'});
    var rows=fields;
    if(rows.length==0){
      var k='Записей удовлетворяющих запосу нет';
      var j=0;
    }
    if((rows.length>0) && (rows.length<=20)){
      var k=rows.length;
      var j=rows.length;
    }
      if(rows.length>20){
      var k=rows.length;
      var j=20;
    }
    var texts='';
    for (let i=0; i<j; i++)
    {
    texts=texts+'\n\r'+rows[i].name;}
    res.write('<!DOCTYPE html>'
        +'<head>'
        +'<meta charset="utf-8">'
        +'<title>Тестовое задание</title>'
    +'<script type="text/javascript" src="new.js"></script>'
        +'</head>'
        +'<body>'
    +'<form name="form1">'
    +'Выберите таблицу:'
    +'<select name="table" size="1">'
    +'<option selected value="table1">table1'
    +'<option value="table2">table2'
    +'<option value="table3">table3'
    +'</select>'
    +'<input type="text" name="string" placeholder="Введите cроку посика">'
    +'<input type="submit" name="submit" value="Поиск" onclick="test();">'
    +'</form>'
    +'<br><br>')
    res.end('<form name="form2">'
    +'<input type="text" name="string" size = "30" value="'+k+'">'
    +' <textarea cols="20" rows="'+2*j+'"  wrap="hard"'
    +'</textarea>'		
    +texts
    +'</textarea>'
     +'</form>'
     +'</body>'
     +'</html>');
    });
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question