www.webdeveloper.com
Results 1 to 5 of 5

Thread: best way to store values to submit to database

  1. #1
    Join Date
    Sep 2008
    Posts
    104

    best way to store values to submit to database

    I've got a table w/ multiple columns of text boxes. Each checkbox has an onClick event that goes to a js function where I want to store the variables that I'm passing to the js function. The variables are a primary key, and a field and a value for that field. I want to store these variables as checkboxes are checked/unchecked, and get all the variables and their values back and update a db when a submit button is clicked.

    Is just a normal js array the best way to do this? Does anyone have any examples?

    Here's my onClick function so far:
    Code:
     
    
    function saveit(projectID,field){
    //alert(projectID + " " + field " " + value);
    
    	if(document.getElementById(projectID).checked == false){ 
    		//stub to store values of checked check boxes
    	}
    
    	if(document.getElementById(projectID).checked == true){ 
    		//stub to store values of checked check boxes
    	}
    
    }

  2. #2
    Join Date
    Mar 2007
    Location
    USA
    Posts
    449
    Sure you could store them as an array, it all depends on what information you want to push out the db entry. If you want lists stored then use an array, otherwise a simple true/false is okay. Also not sure if you're trying this or not, but you'll need server side code to connect to the db, javascript alone won't get you all the way there.

  3. #3
    Join Date
    Aug 2009
    Posts
    25
    In practice, I've found the easiest way to deal with checkboxes is to store the value of the checked elements as a comma deliminated string in a text field.

    Then when re-rendering the page, use the preprocessor-appropriate string search function (VB: InStr, PHP: strpos) to find that value and print the "Checked='Checked'" string if found.

    When it comes time to query the database, simply use a Count/Where LIKE '%,VALUE,%' combo to pull the value out of the string (just remember to pad the front and back of the string with commas)

  4. #4
    Join Date
    Sep 2008
    Posts
    104
    cool, I'll store the values in an array then. to update the db I'll send the array to another page that runs the query.

  5. #5
    Join Date
    Sep 2008
    Posts
    104
    actually, is there a way to get checkbox id's and values when the page loads, and then when I submit can I get all the id's and values again? That way I could compare arrays and only update values that have changed.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles