0

I am using nodejs ,html,express , node-nosql and JavaScript. I have a problem wherin the html page is given below :

<body>
    <form name="form1" method="post" onSubmit="createTable();return myFunction();">
    <input type ="text" name="DomainName" id="Domain_name" required="" />
    <input type="submit" value="Submit" />
     </form>
    <form name="form2" action="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi" method="post">
    <div id="table_container"></div>
    <input type="submit" value="submit" />
    </form>
</body>

The javascriptFunction is as below:

function myFunction() {
       var input_domain = document.forms["form1"]["DomainName"].value;
    if (input_domain == null || input_domain == "") {
        alert("Please enter a valid domain");
        return;
    }
        return false;
    }

    function createTable() {
    document.getElementById("table_container").innerHTML = "";
    var input_domain = document.forms["form1"]["DomainName"].value;
    if (input_domain == null || input_domain == "") return;
    var names = ["website1", "website2"];
    var table = document.createElement("table"),
    ...using javascript dynamically create a table with certain columns for each of the websites listed in the var names list ...
    document.getElementById("table_container").appendChild(table);  
    </script>

The Problem i am facing now is , when i click on submit button of form1 , the
var names = ["website1", "website2"]; has to be retrieved from database . I know i have to use the ajaxrequest with nodejs. But i am at a loss as to how it should be returned and processed ! Please help .

3
  • Sadly, the question you are asking is very very basic. You have to figure out how to make an ajax request to the node.js server, and how node.js will process this request. This is pretty much the basic stuff you need to know about it before you try to work with it. So don't expect to get help to solve your "problem" here. Just go ahead and learn the basics first. Commented Nov 21, 2013 at 6:45
  • @Munim : sadly he put the code block as a comment to the answer below. :P Commented Nov 21, 2013 at 6:49
  • @mithunsatheesh ah didn't notice that. anyway, users are expected to give complete problem if they expect us to help. it just sounded like a person who hasn't tried anything first. Commented Nov 21, 2013 at 8:46

2 Answers 2

0

Commenting on the nodejs code you put as comment

the express route for ajax request could be made like

/** include these require and connect lines if you already dont have it in the code **/
var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
});
connection.connect();

/** express route for ajax request starts **/
$.post("/ajaxTarget",function(data){
    connection.query('SELECT DBName FROM CrawlerDBInfo where Domain =' + "' " + input_domain + " '" + ';', function (error, rows, fields) { 
        if (rows.length == 0) { 
            res.json({error:"no record!!"});
        } else {        
            res.json({data:rows});
        }       
    }   
});

notes:

  1. you are using express, so you can directly use res.json for sending the json response.
  2. no need of looping over the array and building the json result.
  3. also make sure you put the initial mysql require & connect lines in the code.
  4. also think of using the error object available in your mysql query callback.
Sign up to request clarification or add additional context in comments.

Comments

0

I want to tease out exactly what you mean:

  1. The value of the variable names is array of strings
  2. At the start, those strings are in a table on the server side.
  3. When the function is called, you want to retrieve those strings, put them in names, and press on.

Have I got that right?

If so, you have figure out several separate pieces:

  • You need to learn enough about a database to create a table and populate it with those strings.
  • You need to learn enough about Node.js to write a function to retrieve those string
  • You need to learn enough about Node.js to write a function that will serve the results over HTTP
  • You need to learn enough about Javascript (and probably jQuery) to write a function that will make that HTTP request and read the results

Each of those tasks is fairly easy, but obviously, beyond the scope of a single StackOverflow answer.

2 Comments

Yea .you ve got it right. The strings are in a mysql database , I have retrieved them using the following code connection.query('SELECT DBName FROM CrawlerDBInfo where Domain =' + "' " + input_domain + " '" + ';', function (error, rows, fields) { res.writeHead(200, { 'Content-Type': 'text/plain' }); str = '['; for(var i=0;i < rows.length;i++) str = str + '"'+rows[i].DBName +'" ,'; str= str + ']'; res.send(str); But How do i get the str in the client side using javascript .? :(
@rasikavijay : i would help if you could put the code block your question. Show us what you tried.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.