Web Sql - Make select from ID

Asked

Viewed 451 times

2

I’m trying to make a select from a ID who will be caught in the value of a option, I did a search on the net and saw that they put this inside a function, but when I put the function, the variable that should store the value, it just doesn’t work anymore.

That way the code works and even gets the value, but it does not work in the select from DB, everything I’ve tried to do always fails to work.

    var myTest = document.getElementById("list").value;        
    db.transaction(function(tx) {
        tx.executeSql(
        'SELECT * FROM exercicios WHERE se_id = ?', [myTest],
            function(tx, results){
                var exlist = document.getElementById("exlist");

                for(var i = 0; i < results.rows.length; i++) {
                    var row = results.rows.item(i);
                    exlist.innerHTML += "<li>" + row.se_id + " - " + row.ex_nome + "</li>";
                }
            }
        );
    });
  • My friend, beware of input codes mixed with sql queries and html outputs... it’s time to search for an architecture. These code that "even work" one hour will give you a lot of headache.

1 answer

0


By an accident of your own se_id is numerical? When you do:

var myTest = document.getElementById("list").value;

The value of myTest is a string. Use it in select will cause it to return no result if the se_id is numeric. Try transforming this value into an integer before using select:

var myTest = parseInt(document.getElementById("list").value, 10);

Example in jsFiddle.

  • Not yet solved. I put an example in jsFiddle to see if it facilitates: http://jsfiddle.net/jb24d/

  • @Altembergandrade With me it worked - I only had to correct two errors in your fiddle: 1) I put the creation of the tables in one callback of openDatabase for it not to insert new lines every time the test runs; 2) I created the function listExs that you had not created and passed the select code into it. http://jsfiddle.net/mgibsonbr/jb24d/1/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.