0

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..

flicher88
  • 35
  • 2
  • 7
  • 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 '13 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. – sven May 28 '13 at 11:15
  • this may help: http://stackoverflow.com/questions/4997252/get-post-from-multiple-checkboxes – Pete May 28 '13 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 '13 at 11:28

2 Answers2

0

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...);">
Zsolt Szilagyi
  • 4,741
  • 4
  • 28
  • 44
-3

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.

flicher88
  • 35
  • 2
  • 7