Dynamic Form multiplr inputs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Dynamic Form multiplr inputs

Post 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?
nithyavivegam
Forum Newbie
Posts: 4
Joined: Fri Oct 31, 2008 10:16 am

Re: Dynamic Form multiplr inputs

Post 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>
Attachments
patient.zip
(1.15 KiB) Downloaded 5 times
Post Reply