Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have read through many questions and do my searches for hours, but i still cannot find the solution to what i exactly want.

<form method="POST">
  <input type="checkbox" name="fruit" value="0" />No Preference
  <input type="checkbox" name="fruit" value="1" />Apple
  <input type="checkbox" name="fruit" value="2" />Orange
  <input type="checkbox" name="fruit" value="3" />Banana
</form>

print_r($_POST);

I already did validation to checkbox value 0 to 1,2,3 so they will inverse and i did it using looping javascript search for the element name. The problem is, i have to use the element name without changing it become array name. (e.g Fruit => Fruit[] ). So i need to use this element name to retrieve all checked information inputted by the customer. I've seen this can be done in ASP, but i could not figure how they do as it's long time ago already.

My question is, could any one figure how to do this without changing the element name into array format (e.g Fruit => Fruit[] ) ? T.T

Any help will be appreciated. Thank you..

share|improve this question
1  
It is independent of any programming languages as it is HTML. Can you tell us the reason why you want like that only? – Rikesh May 28 at 11:14
1  
What's the problem using the array?? asking because when you'll submit the form, your $_POST['fruit'] can have only one value untill and unless it's an array. – boom_Shiva May 28 at 11:15
this may help: stackoverflow.com/questions/4997252/… – Pete May 28 at 11:18
I don't see why you don't just use name="fruit[]" and then use your Javascript to loop through all elements with fruit[] as their name instead of fruit. That's the easiest way of getting multiple check box values. Are users supposed to be able to check multiple fields? It almost looks like you want to be using a Radio button instead. – MatthewMcGovern May 28 at 11:28

2 Answers

As you already figured, when you submit the same name multiple times, the latest one will overwrite former ones. A solution is to set up a hidden field

<input type="hidden" value="" name="foobar_as_array">

and use jQuery or plain JS to concat your values into that field on submit

<form ... onSubmit=$('#foobar_as_array').value(...concatenate them...);">
share|improve this answer
up vote -1 down vote accepted

I changed the validation using this line :

var GenBox = eval("document.forms['SubmitSearchAll']['" + oCheckBox+"[]']");

and it helps me alot. Now it is solved.

Thanks everyone for contributing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.