2012-10-24 1 views
0

나는 동적 인 형식을 가지고 있습니다.동적 양식의 요소를 에코하는 방법은 무엇입니까?

사용자는 원하는대로 n 개의 필드를 만들 수 있습니다. 그는 형태

여기 코딩 ID

<form method="post" action=""> 
<label>Service</label> 
<select name="service"> 
<option value="1">Purchase</option> 
<option value="2">Sale</option> 
<option value="3">Rent</option> 
</select> 
<label>Number of fields</label><input type="text" name="no_fields"><br> 
<input type="submit" name="submit"> 
</form> 
<?php 
if($_POST['no_fields']>0) 
{ 
    ?> 
    <form method="post" action=""> 
    <?php 
    $j=$_POST['no_fields']; 
    $s=$_POST['service']; 
    for($i=1;$i<=$j;$i++) 
    { 
     ?> 
     <?php echo $i.". "; ?> 
     <label>Name of the field : </label><input type="text" name="name_field[]"> 
     <label>Type : </label> 
     <select name="type[]"> 
     <option>text</option> 
     <option>textarea</option> 
     <option>button</option> 
     <option>radio buton</option> 
     <option>checkbox</option> 
     </select> 
     <br><br> 
     <?php 
    } 
    ?> 
    <input type="hidden" name="serv_id" value="<?php echo $s; ?>"> 
    <input type="hidden" name="loop" value="<?php echo $j; ?>"> 
    <input type="submit" name="save" value="save"> 
    </form> 
    <?php 
} 
if(isset($_POST['save'])) 
{ 
foreach($name_field as $v) 
{ 
echo $v; 
} 
} 
    ?> 

I 양식의 요소를 에코 할의 요소 1,2,3,4 ... 수를 만들 수 있지만이 작업을 수행 할 수 없습니다입니다 . Plz 도와주세요.

답변

0

동일한 이름 필드를 사용하면 문제가 발생할 수 있습니다.

<label>Name of the field : </label><input type="text" name="name_field[]"> 
     <label>Type : </label> 
     <select name="type[]"> 

는 배열 형식의 원인 문제가되지 않습니다

<label>Name of the field : </label><input type="text" name="name_field<?php echo $i;?>[]"> 
     <label>Type : </label> 
     <select name="type<?php echo $i;?>[]"> 

사용 일이

foreach($_POST as $p){ 
echo $p; 
} 
+0

'$ _POST'가 작성해야 대문자. – duri

+0

@jhonraymos : 필드 이름에 $ i를 사용해야하는 이유는 무엇입니까? –

+0

@ Ravneet'Abid '[이보십시오] (http://stackoverflow.com/questions/2203430/posting-form-fields-with-same-name-attribute) – StaticVariable

0

같은 필드 이름이 같아야합니다. 가능한 솔루션입니다 :

foreach($_POST['name_field'] as $nf) //this is for name field 
echo $nf; 
foreach($_POST['type'] as $te) 
echo $te; 
0
if(isset($_POST['save'])) { 
$values = array_values($_POST); 
    foreach($_POST as $v) { 
     print_r($v); 
    } 
} 
관련 문제