SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,338
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How do I prepend text to a JS variable inserted into a DB?

    The following code was taken from http://ofps.oreilly.com/titles/9781449383268/ch05.html, in which one learns to use JS with a database.

    What I'd like to know is how to add some text along with the variable being inserted into the database. For instance, the user may type CARROTS in a "food" field. So CARROTS would go into the food field of the database. What I'd like to do is prepend FOOD: to the term the user types in so they will be paired together in the database, and later seen together when pulled from the db: FOOD: CARROTS

    This is the code where the user's input is added to the db. How do I modify it to prepend a text to the user's input? I'm not at all proficient at JS. This is the SQLite database.

    Thanks!


    Code:
    function createEntry() {
        var date = sessionStorage.currentDate;
        var calories = $('#calories').val();
        var food = $('#food').val();
        db.transaction(
    				   function(transaction) {
    				   transaction.executeSql(
    										  'INSERT INTO entries (date, calories, food) VALUES (?, ?, ?);', 
    										  [date, calories, food], 
    										  function(){
    										  refreshEntries();
    										  jQT.goBack();
    										  }, 
    										  errorHandler
    										  );
    				   }
    				   );
        return false;
    }
    
    ... some code here ...
    
    function(transaction) {
    				   transaction.executeSql(
    										  'SELECT * FROM entries WHERE date = ? ORDER BY food;', 
    										  [currentDate], 
    										  function (transaction, result) {
    										  for (var i=0; i < result.rows.length; i++) {
    										  var row = result.rows.item(i);
    										  var newEntryRow = $('#entryTemplate').clone();
    										  newEntryRow.removeAttr('id');
    										  newEntryRow.removeAttr('style');
    										  newEntryRow.data('entryId', row.id);
    										  newEntryRow.appendTo('#date ul');
    										  newEntryRow.find('.label').text(row.food);
    										  newEntryRow.find('.calories').text(row.calories);
    										  newEntryRow.find('.delete').click(function(){
    																			var clickedEntry = $(this).parent();
    																			var clickedEntryId = clickedEntry.data('entryId');
    																			deleteEntryById(clickedEntryId);
    																			clickedEntry.slideUp();
    																			});
    Last edited by StevenHu; Mar 17, 2011 at 13:56. Reason: added more code

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    I have no idea why you would want to do that (you could as well append it after selecting the data from the database and before displaying it, keeping the data in the database "clean"), but all you have to do is change this line
    Code:
    var food = $('#food').val();

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,338
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I need to prepend the text because the fields will be output on another page.

    So what do I change the line to?

    Thanks!

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,338
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I was thinking of prepend instead of concatenate. Thanks for the tip!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •