Page 1 of 1

Dynamic Form multiplr inputs

Posted: Sun Nov 02, 2008 3:53 pm
by odie2828
I am trying to create a for that you can select the number of patients information that you are going to input.

The form loads with an area for one patient to enter their information.
There is a spot on the form for people to select how many patients that they are going to enter information for.
It is a drop down menu that is 1-5

I have created a table that contains all of the information that needs to be fillled out for each patient.

If the person selects 3 from that drop down menu then i want that form to repeat itself 3 times.

Any Suggestions or can someone point me to a tutorial so i can learn?

Re: Dynamic Form multiplr inputs

Posted: Mon Nov 03, 2008 10:31 am
by nithyavivegam
I hope this might be of some help to you. You don't need to create multiple forms to get multiple patient information.
I have only shown a very basic thing you could try. You can extend this however you need.
I did not add any comments (bad practice). Find the attchment for the two files.




sel_patient_count.php

Code: Select all

<?php
    if ( isset($_POST['sel_num_of_patient']) && $_POST['sel_num_of_patient'] != '' ) 
    {
        $patient_count = (int) $_POST['sel_num_of_patient'];
        if ($patient_count > 0) 
        {
            header("Location: patient.php?p_cnt=$patient_count");
            die();
        }
        else
        {
            echo 'Please choose the number of patients';            
        }
    }
?>
 
<form name="frm_num_of_patient" method="post" action="<?=$_SERVER['PHP_SELF'];?>">
    Choose How many patients 
    <select name="sel_num_of_patient" id="sel_num_of_patient">
    <option value="0">Choose</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select>
    <input type="submit" name="submit" value="Submit">
</form>

patient.php

Code: Select all

<?php
    ob_start();
    if ( isset($_GET['p_cnt']) && $_GET['p_cnt'] != '' ) 
    {
        $p_cnt = (int)$_GET['p_cnt'];
    }
    elseif (isset($_POST['cmd']) && $_POST['cmd'] == 'patient_process') 
    {
        // add your database connections include("dbinfo.php") soemthing like this
        $p_cnt = (int)$_POST['p_cnt'];
        for ($i = 1 ; $i <= $p_cnt ; $i++) 
        {
            $p_name = $_POST["p_name$i"];
            $sql = "insert into patients (p_name) values ('$p_name')";
            /* execute mysql queries in this block */
            echo $sql . "<br>";
        }
    } 
    else
    {
        header("Location: sel_patient_count.php");
        die();
    }
    
?>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<?php
    for ($i = 1 ; $i <= $p_cnt ; $i++)
    {
?>
<h2>Patient <?=$i;?></h2>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Patient Name:</td>
    <input type="textbox" name="p_name<?=$i;?>">
</tr>
</table>
<br>    
<?php       
    }
?>
<input type="hidden" name="cmd" value="patient_process">
<input type="hidden" name="p_cnt" value="<?=$p_cnt;?>">
<input type="submit" name="submit" value="Submit">
</form>