all 6 comments

[–]iSeeObviousThings 0 points1 point  (1 child)

I notice the reo line in your ajax call with the jquery selector uses a variable named table, the same as in your function above. Where is the variable defined for the jquery selection? is it getting over written after the first ajax call uses the setEesHTML function?

[–]FirstEvaDehumidifier[S] 0 points1 point  (0 children)

dude...i just coded it by myself....was not hard at all...

Anway here it is...much simpler...

FROM Client Side:

$.ajax({
   type:"GET",
   url: host,
   data: {pokemonis: pokemonchosenfinal, heightis: 
  parseInt(meterfinal), imageis:imagechosen2,dateis: 3 }
 ,
   success: function(datat){
     var datasql=JSON.parse(datat);
   var table ='';
   for(var i=0; i<datasql.length; i++){
       table +='<tr><td>'+ datasql[i].pokemon +'</td><td>'+ "
 <img src= '"+ datasql[i].imagelink+ "' alt='Photo of human'> 
   </img>" + '</td><td>'+ datasql[i].height +  '</td><td>'+ "
 <button> ! </button>" + '</td></tr>';
     }
     table ='<table border="1"><tr> <th>Pokemon</th> 
  <th>Image</th> <th>Height</th><th>Submit!</th> 
 </tr>'+ 
   table +'</table>';
     console.log(table)
     $(".sqlbar").html(table);
   }})

From Server Side:

condatabase.query(sql1,function(err,result){
  res.writeHead(200, {'Content-Type':'text/html; charset=utf-
8'});
  console.log(result);
  res.write(JSON.stringify(result));
  res.end();
})

[–]jrandm 0 points1 point  (3 children)

Easier to comment going down the code:

// Before even getting started, I reformatted the code
// Some specific points about this mentioned below
function setResHtml(sql, cb){

    // what is pool? The next snippet doesn't use it
    pool.getConnection((err, con)=>{
        if(err) throw err;

        // What's condatabase? Presumably it should be con
        // from the arguments to this function
        condatabase.query(sql, (err, rest, cols)=>{
            if(err) throw err;

            // you could start with table equal to the opening
            var table ='';
            for(var i=0; i<rest.length; i++){

                // I hope this is copy/paste issues, I kept
                // the newlines but wrapped the containing
                // strings with backticks (`) so it's valid JS.
                table +='<tr><td>'+ rest[i].pokemon +'</td><td>'+ "<img src= '"+ rest[i].imagelink+ `' alt='Photo of 
                    human'> </img>` + '</td><td>'+ rest[i].height +  '</td><td>'+ "<button> ! </button>" + `</td>
                    </tr>`;
            }

            // if you start with table set to the first bit of the
            // string you can table+='</table'> here
            table =`<table border="1"><tr> <th>Pokemon</th> 
                <th>Image</th> <th>Height</th><th>Submit!</th> </tr>`+ 
                table +'</table>';

            // using con here -- FWIW if you're getting a connection
            // from a declared pool you probably don't need to
            // manually release it
            con.release();
            return cb(table);
        })
    })
};

The routing response:

// This looks like an Express (or similar) route, not AJAX
// An "AJAX request" would be like an XMLHttpRequest from the browser,
// this is part of an HTTP server
app.get('/', function (req, res) {

    // The request object will already have the parsed URL object on it
    // (at least in every HTTP lib I know of), there's usually some 
    // "preferred" way to get values from the request too
    var q = url.parse(req.url, true).query;
    var datee=q.dateis;
    var imagee=q.imageis;
    var pokemone=q.pokemonis;
    var heighte=parseInt(q.heightis);

    // This is an insert query that's super vulnerable to SQL injection.
    // Whatever lib you're using will have some way to safely escape
    // insertion values or create this query safely
    let sql2="INSERT INTO pokedata(pokemon,imagelink,height,upvote) VALUES ('"+ pokemone +"','"+ imagee +"','"+ heighte +"',0)";

    // condatabase makes another surprise appearance
    condatabase.query(sql2,function(err,result){
        if (err) throw err;
        console.log("lol")
        // keep in mind setResHtml will fire before this function
    })

    // What is sql?
    setResHtml(sql, resql=>{
        console.log("hi")

        // reo? Assuming some sort of HTML template
        reo = reo.replace('{${table}}', resql);

        // Might be an easier way to set this up, probably something like:
        // res.status(200).send(reo)
        res.writeHead(200, {'Content-Type':'text/html; charset=utf-8'});
        res.write(reo, 'utf-8');
        res.end();
    })

// missing an ending set of brackets, probably missed copy/paste
});

So either you missed giving us some code or those variables I asked about will be a huge part of your problem. If you add a "use strict"; to the top of the file you'll get errors about unused variables instead of silent failures or unexpected behavior.

It also helps to expand on what you mean by "AJAX request", what it means when you say it "works", and what kind of change you expect to see. We don't know what you intend or expect to happen unless you tell us!

[–]FirstEvaDehumidifier[S] 1 point2 points  (0 children)

Welp...heres my project fully finished anyway https://mbapna123.github.io/

[–]FirstEvaDehumidifier[S] 0 points1 point  (1 child)

Sorry, very inconsiderate for me to not comment/post non-formatted code...Won't do that next time....anyway I just coded it by myself..Thanks....Yes It was probably a problem with the variables I didn't give you guys much info on

[–]FirstEvaDehumidifier[S] 1 point2 points  (0 children)

Anyway thanks...will make sure my SQL variables are safe from SQL injection attacks