13 Replies - 12726 Views - Last Post: 15 May 2012 - 02:09 AM

#1 haha1132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-April 10

get value from checkbox: HTML with javascript and store in websql

Posted 14 May 2012 - 08:33 PM

I had a problem on how to get the value from checkbox after checked, because i declare all the input checkbox the same id, so it just will check the first value that when i set all with same id, so how im gonna get other value beside than test1, the code like below, testc is the checkbox id
var testc = document.getElementById('testc');
function inserttab(){
		db.transaction(function(tx){
			tx.executeSql(insertAll,[firstName.value,lastName.value,phone.value,email.value,selectT.value,testc.value],loadAndReset,onerror);
		});
	}


html form code
form id="cb">
     <input type="checkbox" name="type[0]" value="test1" id="testc"/>test1<br/>
     <input type="checkbox" name="type[1]" value="test2" id="testc"/>test2<br/>
     <input type="checkbox" name="type[2]" value="test3" id="testc" />test3<br/>
     </form>


Attached Image
the attach file is the result i get, i noe if using php i get the answer but i want to use html and javascript base to do it, so please tell me how the code will be to save other value.

Is This A Good Question/Topic? 0
  • +

Replies To: get value from checkbox: HTML with javascript and store in websql

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 14 May 2012 - 11:16 PM

you’re violating one of HTML’s basic rules: "IDs must be unique".

therefore, with this code, you won’t get it to work. either you give each <input> a different ID or you access them through their names.
Was This Post Helpful? 0
  • +
  • -

#3 haha1132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-April 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 15 May 2012 - 01:19 AM

View PostDormilich, on 14 May 2012 - 11:16 PM, said:

you’re violating one of HTML’s basic rules: "IDs must be unique".

therefore, with this code, you won’t get it to work. either you give each <input> a different ID or you access them through their names.


ya, so i trying to use this code
var element_1 = document.getElementsByName('element_1');
function getValue(){
		for(var x=0; x<element_1.length;x++){
			if(element_1[x].checked==true){
				element_1[x].value;
			}
		}
	}
function inserttab(){
		db.transaction(function(tx){
			tx.executeSql(insertAll,[firstName.value,lastName.value,phone.value,email.value,selectT.value,testc.value,getValue()],loadAndReset,onerror);
		});
	}

but i cant get the value in the database, can you define which code should i place and replace? i just want to get the answer urgent, thanks

This post has been edited by Dormilich: 15 May 2012 - 01:20 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 15 May 2012 - 01:22 AM

uh, getValue() doesn’t have a return value (which you would need in the second function). essentially, line #5 calls the value and does nothing with it.

and there’s another problem. since you’re using checkboxes, you can select multiple boxes. which value do you use in the query?

This post has been edited by Dormilich: 15 May 2012 - 01:24 AM

Was This Post Helpful? 0
  • +
  • -

#5 haha1132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-April 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 15 May 2012 - 01:25 AM

View PostDormilich, on 15 May 2012 - 01:22 AM, said:

uh, getValue() doesn’t have a return value (which you would need in the second function). essentially, line #5 calls the value and does nothing with it.

that's why i need you all help, do you have any link or code can help me fix this simple thing? i just want the the checkbox value which is checked and store in websql database by using this code, a way of help will appreciate, i definitely first time use checkbox and want to store in database. thanks alot
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 15 May 2012 - 01:26 AM

use the return statement.
Was This Post Helpful? 1
  • +
  • -

#7 haha1132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-April 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 15 May 2012 - 01:31 AM

View PostDormilich, on 15 May 2012 - 01:22 AM, said:

uh, getValue() doesn’t have a return value (which you would need in the second function). essentially, line #5 calls the value and does nothing with it.

and there’s another problem. since you’re using checkboxes, you can select multiple boxes. which value do you use in the query?


ya you are right, so i try to change it to radiobutton, so the coding is still remain the same?
Was This Post Helpful? 0
  • +
  • -

#8 haha1132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-April 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 15 May 2012 - 01:47 AM

View PostDormilich, on 15 May 2012 - 01:26 AM, said:

use the return statement.

thanks man!! it is work if use radiobutton!! love you mentor!! but i still dont know how to use to get value in checkbox since it is multiple choices...
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 15 May 2012 - 01:50 AM

it depends on how you want to handle the multiple results.
Was This Post Helpful? 0
  • +
  • -

#10 haha1132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-April 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 15 May 2012 - 01:56 AM

i have no idea on that now,my head is pain, is it got any tutorials or example for get checkboxes value and store in websql?
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 15 May 2012 - 02:00 AM

if you want to store all selected values then you need to insert each value separately, which brings loops into use.
Was This Post Helpful? 0
  • +
  • -

#12 haha1132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-April 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 15 May 2012 - 02:02 AM

which mean that save it with select different id? then the table inside should separate to store them?
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 15 May 2012 - 02:05 AM

View Posthaha1132, on 15 May 2012 - 11:02 AM, said:

which mean that save it with select different id? then the table inside should separate to store them?

???

you can use IDs ... how your storage table handles this is out of my knowledge.
Was This Post Helpful? 0
  • +
  • -

#14 haha1132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-April 10

Re: get value from checkbox: HTML with javascript and store in websql

Posted 15 May 2012 - 02:09 AM

ok..i think i get it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1