Showing posts with label Passing Array. Show all posts
Showing posts with label Passing Array. Show all posts

Passing JavaScript Array to PHP by Using POST

Passing JavaScript Array to Php is not very popular. However, in some circumstances it becomes necessary; for instance, passing value of a group of checkboxes, using array would be most suitable setting.

In general, passing array is same as passing a variable. In HTML part, it looks:

<input type="checkbox" name="jsArray[1]" value="firstValue">
<input type="checkbox" name="jsArray[2]" value="secondValue">
<input type="checkbox" name="jsArray[3]" value="thirdValue">

The problem happens when HTML codes are generated dynamically or the value of checkbox is assigned dynamically. Normal assignment method, namely document.formName.inputItemName.value = something does not work. There is a suggestion in http://www.it-base.ro/2007/07/27/send-a-javascript-array-to-php/.

Another simply solution is to use id to assign the value:

; document.getElementById('inputItemId').value = something

Easy.

Labels