'form'에 해당되는 글 1건

  1. 2010.06.24 Form과 Input 태그로 배열 넘기기
WebApp/PHP2010. 6. 24. 01:08
form과 input 태그를 통해서 배열을 넘기는 것을 의외로 간단하다.
input 태그에 있는 name 속성에 변수명 및 대괄호[]를 이용해서 작성한다.
다음 예제를 통해서 확인하자.
<form action='http://www.example.com/' method='post'>
<p>
<label>array[0]</label>
<input type='text' name='array[]' value='hello' />
<br />
<label>array[1]</label>
<input type='text' name='array[]' value='world' />
<br />
<label>array[2]</label>
<input type='text' name='array[]' value='!' />
<br />
<input type='submit' value='Submit' />
</p>
</form>

위의 예제를 통해서 값을 전송하게 되면, PHP와 같은 경우 위의 값을 다음 코드를 통해 출력할 수 있다.
print_r($array);
또는
print_r($_POST['array']);
의 두 가지 방법 모두 가능하다. 하지만 Form태그의 Method 값이 GET인 경우는 배열을 넘길 수가 없으니 $_GET은 무용지물임을 명심하기 바란다.

상단 코드의 실행 값은 다음과 같다,
Array ( [0] => hello [1] => world [2] => ! )

숫자를 이용한 자동 배열 뿐만 아니라 문자로 지정한 배열명도 사용이 가능하다.
<form action='http://www.example.com' method=''>
<p>
<label>Identification</label>
<input type='text' name='user[id]' value='hello' />
<br />
<label>Password</label>
<input type='password' name='user[pw]' value='world' />
<br />
<input type='submit' name='Sign-in' />
</p>
</form>
위와 같이 제출 폼을 작성하면 print_r함수로 다음과 같은 값을 얻을 수 있다.
Array ( [user] => Array ( [id] => hello [pw] => world ) )

'WebApp > PHP' 카테고리의 다른 글

다차원배열의 구성  (0) 2010.06.26
array_merge()  (0) 2010.06.25
파일 경로 숨긴 채로 전송 및 다운로드하기  (0) 2010.06.07
파일 읽기 함수  (0) 2010.05.30
PHP 자바스크립트 경고 함수  (0) 2010.05.30
Posted by 마이클