0

I have a dynamic form.

A user can create n number of fields as he wants. he can create 1,2,3,4... number of elements in the form

coding id here

<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 want to echo the elements of the form but I am unable to do this. Plz help me.

Ravneet 'Abid'
  • 87
  • 3
  • 4
  • 7

3 Answers3

0

using the same name field may create problem.

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

should be

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

use something like this

foreach($_POST as $p){
echo $p;
}
StaticVariable
  • 5,253
  • 4
  • 23
  • 45
0

Same field name should not be a problem cause it's in array format. Probable solution is:

foreach($_POST['name_field'] as $nf) //this is for name field
echo $nf;
foreach($_POST['type'] as $te)
echo $te;
kidz
  • 308
  • 1
  • 8
0
if(isset($_POST['save'])) {
$values = array_values($_POST);
    foreach($_POST as $v) {
        print_r($v);
    }
}
Nipun Tyagi
  • 878
  • 9
  • 26