Help with Dynamic Forms

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
Drugoholic
Forum Newbie
Posts: 2
Joined: Thu Jul 30, 2009 3:17 am

Help with Dynamic Forms

Post by Drugoholic »

Hello everyone,

I'm having one annoying problem with a script I didn't even write.
First of all, let me just point out the issue;

If you goto this address (site is in french): LINK
You'll notice that it's a dynamic calculator, if you choose "individual" and click next, it'll show you just 1 row but if you choose "family of two, three or more" it'll show you extra rows depending on the number you've picked.

What I wanna do here is IF the user chooses "family of two" or more, to separate the table in the next step by making the rows "Age" and "NSSF" the only variable fields while when he selects the "Class Type" for one of the family, it should be applied to all the rest at once.

I hope I wasn't too confusing in this.

For example, if I choose "family of 3" and click next, the next table should be split into 2, first the user has to:

- "Class Type" which will be selected only once but applied to the 3 persons in the family.
- "Couverture Ambulatoire"
- "Couverture Médicament "
- "Couverture Visites Médicales"

After the user finishes making his selection, he should then find another table below where he'll be able to select the "Age Range" and "NSSF" for each of the 3 persons separately. I can't be any clearer than this, I did my best

Anyway, here is the code which is all found in 1 single file. (find it below)

Help is pretty much appreciated... thanks.

Code: Select all

<? 
 
$step = $_POST["step"];
 
if($_POST['sendmail']){
        
        $amnt= $_POST['amnt'];
        $amnt_amb = $_POST['amnt_amb'];
        $amnt_med = $_POST['amnt_med'];
        $amnt_doc = $_POST['amnt_doc'];
        $sum = $_POST['sum'];
        $counter = $_POST['nb'];
        $total = $_POST['total'];
        $nom = $_POST["nom"];
        $prenom = $_POST["prenom"];
        $phone = $_POST["phone"];
        
        
        $table ='<table align="center" cellpadding="3" cellspacing="0" border="1">
                    <tr id="policyheader">
                        <td id="policytd">#</td>
                        <td id="policytd">Montant Police(US$)</td>                        
                        <td id="policytd">Couverture Ambulatoire (US$)</td>
                        <td id="policytd">Couverture M&eacute;dicament (US$)</td>
                        <td id="policytd">Couverture Visites M&eacute;dicales (US$)</td>
                        <td id="policytd">Total Police (US$)</td>
                    </tr>';
        
        ksort($counter);
        foreach ($counter as $nb=>$data){
            
            $table.= "<tr>
                    <td id='policytd'>".number_format($nb,2,'.',',')."</td>
                    <td id='policytd'>".number_format(($amnt[$nb]?$amnt[$nb]:" "),2,'.',',')."</td>
                    <td id='policytd'>".number_format(($amnt_amb[$nb]?$amnt_amb[$nb]:" "),2,'.',',')."</td>
                    <td id='policytd'>".number_format(($amnt_med[$nb]?$amnt_med[$nb]:" "),2,'.',',')."</td>
                    <td id='policytd'>".number_format(($amnt_doc[$nb]?$amnt_doc[$nb]:" "),2,'.',',')."</td>
                    <td id='policytd'>".number_format($sum[$nb],2,'.',',')."</td>
                    
            </tr>";    
            
            
            
        }
        $table.= "<tr>
                <td align='right' colspan='5' id='policytd'><b> &nbsp;&nbsp;&nbsp; Total des primes:</b></td>
                <td id='policytd'><b>".number_format($total,2,'.',',')."</b></td>
         </tr>
         ";
    
        $table.= "</table>";
        
        $table.= "<br>Nom : $nom <br>prenom : $prenom <br>Phone : $phone</center>";
        
        
        
        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
        
        /* additional headers */
        $headers .= "From: Axa Middle East <mail@mail.com>\r\n";
 
        //mail("mail@mail.com","Calculez vos primes d'assurances medicales ","$table",$headers);
        mail("mail@mail.com","Calculez vos primes d'assurances medicales ","$table",$headers);
    header("Location:sent.html");
 
        exit;
    }
 
if(!$step){
 
    ?>
        <html>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>.:AXA:.</title>
    <link href="styl.css" rel="stylesheet" type="text/css" />
    
    </head>
    <body>
    <script language="JavaScript">
 
    function checkVar(form)
    {
        
        nb_family_id = form.nb_family_id.value;
        
        if(nb_family_id=="")
        {
            alert("Choisissez le nombre de personne que vous désirez assurer.");
            return false;
        }
        
        
        else return true;
    }
    
    </script>
 
    <form method="post" name="frm1" enctype="multipart/form-data" onSubmit="return checkVar(this);">
    <table>
            <tr>
                <td id="maintable">
                    <table width="100%" cellpadding="6" cellspacing="6">
                        <tr>
                            <td colspan="3" width="100%" id="header" style="font-family:tahoma; font-size:14px">
                            <strong>Assurances M&eacute;dicales</strong></td>
                        </tr>
                        <tr>
                            <td style="font-size:12px; font-family:tahoma" width="65%">
                            Combien de personne(s) voulez-vous assurer?                            </td>
                            <td>
                                    <?
                                        $query="select id,nb_family from family_type order by id";
                                        
                                        echo "<center>";
                                        comboBox($query,"nb_family_id",null,true);
                                        
                                        ?>
                            </td>
                            <td rowspan="3" valign="top"> <img src="6.gif"/></td>
                        </tr>
                        
                        <tr>
                            <td colspan="2" align="left">            
                                    <input type="hidden" value="1" name="step"/>
                                    <input type="submit" value="&Eacute;tape suivante" name="sbm"/></td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
 
        
    </table>
 
    </center>
    <?  
    if(!$step){echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />";}
    ?>
    
    </form>
    
    </body>
    
    </html>    
    <?
    exit();
}
if($step==2){
    //printR($_POST);        
    $age_range_id = $_POST["age_range_id"];
    $class_type_id = $_POST["class_type_id"];
    $nssf = $_POST["nssf"];
    $amb = $_POST["amb"];
    $med = $_POST["med"];
    $doc = $_POST["doc"];
    $counter = $_POST["counter"];
    $family_type_id = $_POST["family_nb"];
    
    
    $policy_title=id2fields("family_type","nb_family","id=$family_type_id"); 
    
    
    
    ?>
 
    
        
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <link href="styl.css" rel="stylesheet" type="text/css" />
    <script language="JavaScript">
 
    function checkVar2(form)
    {
        
        nom = form.nom.value;
        prenom = form.prenom.value;
        phone = form.phone.value;
        
        if(nom=="")
        {
            alert("Veuillez entrez votre nom.");
            return false;
        }
        else if(prenom=="")
        {
            alert("Veuillez entrez votre prénom.");
            return false;
        }
        else if(phone=="")
        {
            alert("Veuillez entrez votre numéro de cellulaire.");
            return false;
        }
        
        
            else return true;
    }
    
    </script>
    </head>
    
    <form method="post" name="frm" enctype="multipart/form-data" onSubmit="return checkVar2(this);">
    
    <?
    echo "<p  style='font-size: 8pt; line-height:11pt; font-family:tahoma'>Les primes ci-dessous sont bas&eacute;es sur une assurance m&eacute;dicale - <b>$policy_title</b>.</p>";
    
    ?>
    
    <center>
    <table align="center" cellpadding="3" cellspacing="0" id="policytable">
        <tr id="policyheader">
            <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">#</td>
            <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">Montant Police(US$)</td>                        
            <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">Couverture Ambulatoire (US$)</td>
            <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">Couverture M&eacute;dicament (US$)</td>
            <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">Couverture Visites M&eacute;dicales (US$)</td>
            <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">Total Police (US$)</td>
        </tr>
    
    
    <?
    ksort($counter);
    foreach ($counter as $nb=>$data){
        
        //echo $age_range_id[$nb].'--'.$class_type_id[$nb].'--'.$nssf[$nb].'--'.$amb[$nb].'--'.$med[$nb].'--'.$doc[$nb].' ||||||| ';
        
        if((!$age_range_id[$nb])||(!$class_type_id[$nb]))continue;
        if($nssf[$nb]=="on")$add="and nssf=1";
        else $add="and nssf=0";
        
        if($amb[$nb]=="on")$get_amount_amb=id2fields("amb_plan","amount","family_type_id=$family_type_id and age_range_id =".$age_range_id[$nb]."");
        if($med[$nb]=="on")$get_amount_med=id2fields("pre_med_plan","amount","family_type_id=$family_type_id and age_range_id =".$age_range_id[$nb]."");
        if($doc[$nb]=="on")$get_amount_doc=id2fields("doc_plan","amount","family_type_id=$family_type_id and age_range_id =".$age_range_id[$nb]."");
    
        
        if($class_type_id[$nb]<>5){ // NO class SP
        
            $get_amount=id2fields("policy","amount",
                "age_range_id=".$age_range_id[$nb]." and class_type_id=".$class_type_id[$nb]." and family_type_id=$family_type_id $add");
        
        }
        else { // Class SP
            
            $get_amount=id2fields("policy","amount",
                    "age_range_id=".$age_range_id[$nb]." and class_type_id=".$class_type_id[$nb]." $add");
 
        }
        $sum_amount=$get_amount+$get_amount_amb+$get_amount_med+$get_amount_doc;    
        /*echo "<b>".($get_amount?$get_amount:"NaN").($get_amount_amb?"- $get_amount_amb ":"").($get_amount_med?" - $get_amount_med ":"").($get_amount_doc?" - $get_amount_doc total : ----> $sum_amount ":"")."</b>";
            echo "<br>";*/
            
        echo "<tr>
                    <td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".$nb."</td>
                    <td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".($get_amount?number_format($get_amount,2,'.',','):"NAN")."</td>
                    <td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".($get_amount_amb?$get_amount_amb:"&nbsp; ")."</td>
                    <td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".($get_amount_med?$get_amount_med:"&nbsp; ")."</td>
                    <td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".($get_amount_doc?$get_amount_doc:"&nbsp; ")."</td>
                    <td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".number_format($sum_amount,2,'.',',')."</td>
                    <input type='hidden' name='amnt[$nb]' value='$get_amount'/>
                    <input type='hidden' name='amnt_amb[$nb]' value='$get_amount_amb'/>
                    <input type='hidden' name='amnt_med[$nb]' value='$get_amount_med'/>
                    <input type='hidden' name='amnt_doc[$nb]' value='$get_amount_doc'/>
                    <input type='hidden' name='sum[$nb]' value='$sum_amount'/>
                    <input type='hidden' name='nb[$nb]' value='$nb'/>
            </tr>";    
            
        $total+=$sum_amount;    
    }
    echo "<tr>
                <td align='right' colspan='5' id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'><b> &nbsp;&nbsp;&nbsp; Total des primes:</b></td>
                <td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'><b>".number_format($total,2,'.',',')."</b></td>
         </tr>
         ";
    
    echo "</table></center><br><br>";
    
    echo '
            <table> 
                    <tr><td style="font-size: 8pt; line-height:11pt; font-family:tahoma" colspan="2" id="seperator">Veuillez remplir les informations suivantes pour qu’un de nos agents vous contacte. </td></tr>
                    <tr><td style="font-size: 8pt; line-height:11pt; font-family:tahoma">Nom :</td><td align="left"> <input type="text" name="nom"/></td>
                    </tr>
                    <tr>
                        <td style="font-size: 8pt; line-height:11pt; font-family:tahoma">Pr&eacute;nom :</td><td align="left"> <input type="text" name="prenom"/></td>
                    </tr>
                    <tr>
                        <td style="font-size: 8pt; line-height:11pt; font-family:tahoma">No.Cellulaire :</td><td align="left"> <input type="text" name="phone"/></td>
                    </tr>
                    <tr>
                        <td colspan="2">&nbsp;</td>
                    </tr>
                    <tr>
                        <td style="font-size:12px; font-family:tahoma" width="60%" id="lasttdpadd" colspan="2">
                            <input type="submit" value="Soumettre" name="submit"/>
                            <input type="hidden" value="true" name="sendmail"/>
                            <input type="hidden" value="'.$total.'" name="total"/>
                        </td>
                    </tr>
                </table><br><br><br><br><br><br><br><br><br><br><br><br><br></form>
                ';
    exit;
                
}
            
if(($_POST["step"]==1)||($_POST["class_type_id"])){
    //printR($_POST);
    ?>
    <script type="text/javascript">
    function AjaxFunction(cat_id,newRI) {
        
        var httpxml;
        try {
          // Firefox, Opera 8.0+, Safari
          httpxml=new XMLHttpRequest();
          }
          catch (e)
          {
          // Internet Explorer
              try
                     {
                     httpxml=new ActiveXObject("Msxml2.XMLHTTP");
                    }
             catch (e)
                    {
                        try
                              {
                              httpxml=new ActiveXObject("Microsoft.XMLHTTP");
                              }
                        catch (e)
                                  {
                                      alert("Your browser does not support AJAX!");
                                      return false;
                                  }
                      }
          }
        
          function stateck() {
            if(httpxml.readyState==4) {
                document.getElementById("age_range_id["+newRI+"]").innerHTML=httpxml.responseText;
            }
        }
           
            var url="pol_ajax.php";
            url=url+"?cat_id="+cat_id;
            url=url+"&newRI="+newRI;
            url=url+"&sid="+Math.random();
            httpxml.onreadystatechange=stateck;
            httpxml.open("GET",url,true);
            httpxml.send(null);
          }
    </script>
 
    
    <?
    $nb_family_id = $_POST["nb_family_id"];
                
    $policy_title=id2fields("family_type","nb_family","id=$nb_family_id");     
    $max_family_type = id2fields("family_type","max(id)as id"); //to show the [add row]
    
    
    //////////////////////////////////////////////////////////////////////////// Option 1 in add row
    $query="select id,range from age_range order by id";
        $res=mysql_query($query);
        $n = mysql_num_rows($res);
            
    $option="<option> </option>";
            
        for($i=0;$i<$n;$i++)
        {
            $row = mysql_fetch_array($res);
            $range = $row["range"];
            $id= $row["id"];
            $option.="<option value='$id'>$range </option>";
            
        }
    //////////////////////////////////////////////////////////////////////////// End option1    
    //////////////////////////////////////////////////////////////////////////// Option 2 in add row
    $query2="select id,typec from class_type order by id";
    $res2=mysql_query($query2);
    $n2 = mysql_num_rows($res2);
            
    $option2="<option> </option>";
            
        for($i=0;$i<$n2;$i++)
        {
            $row = mysql_fetch_array($res2);
            $typec = $row["typec"];
            $id= $row["id"];
            $option2.="<option value='$id'>$typec </option>";
            
        }
    
    //////////////////////////////////////////////////////////////////////////// End option2    
    
    
    
    //$option="<option> </option><option value='1'>14d - 17y </option><option value='2'>18y - 35y </option><option value='3'>36y - 45y </option><option value='4'>46y - 55y </option><option value='5'>56y - 60y </option><option value='6'>61y - 65y </option><option value='7'>66y - 70y </option><option value='8'>71y - 75y </option>";
    //$option2="<option> </option><option value='2'>Class X</option><option value='3'>Class A</option><option value='4'>Class B</option><option value='5'>Class SP</option>";
?>
    
    
<head>
 
<link href="styl.css" rel="stylesheet" type="text/css" />
    
    </head>
<script language="JavaScript">
function addArticle()
    {
        
        
        var atable = document.getElementById("policytable");
        if(atable == null)
        {
            return;
        }
        
        var newRI = atable.rows.length; // new rowIndex
        var aRow = atable.insertRow( atable.rows.length ); 
        var aCol = aRow.insertCell( aRow.cells.length ); 
        
        //aCol = aRow.insertCell();
        aCol.className = "policytd";
        aCol.innerHTML = "<input type=\"hidden\" name=\"counter[" + newRI + "]\" value=\"true\"><span style=\"font-size: 10pt; line-height:11pt;  font-family:tahoma;\" id=\"spn_id_" + newRI + "\">" + newRI + "</span>";
        
        
        aCol = aRow.insertCell( aRow.cells.length );
        aCol.className = "policytd";
        
        aCol.innerHTML = "<select name=\"class_type_id[" + newRI + "]\" onChange='AjaxFunction(this.value,"+ newRI +");'><?echo $option2;?></select>";
        
        aCol = aRow.insertCell( aRow.cells.length );
        aCol.className = "policytd";
        aCol.innerHTML = "<div id=\"age_range_id[" + newRI + "]\" name =\"age_range_id[" + newRI + "]\" ></div>";
        
        aCol = aRow.insertCell( aRow.cells.length );
        aCol.className = "policytd";
        aCol.innerHTML = "<center><input type=\"checkbox\" name=\"nssf[" + newRI + "]\"></center>";
        
        aCol = aRow.insertCell( aRow.cells.length );
        aCol.className = "policytd";
        aCol.innerHTML = "<center><input type=\"checkbox\" name=\"amb[" + newRI + "]\"></center>";
        
        aCol = aRow.insertCell( aRow.cells.length );
        aCol.className = "policytd";
        aCol.innerHTML = "<center><input type=\"checkbox\" name=\"med[" + newRI + "]\"></center>";
        
        aCol = aRow.insertCell( aRow.cells.length );
        aCol.className = "policytd";
        aCol.innerHTML = "<center><input type=\"checkbox\" name=\"doc[" + newRI + "]\"></center>    ";
        
        
    }
    </script>
    
    <?
    
    if($nb_family_id==$max_family_type){
        
        ?>    
        
        <span style="font-size: 8pt; line-height:11pt;  font-family:tahoma;" onClick="addArticle();" onMouseOver="this.style.cursor="pointer";"> [Ajouter un individu]</span><br /><br />
        <?
    
    }
    ?>
 
    <form action="" method="POST">
    
    <table align="center" cellpadding="3" cellspacing="0" id="policytable">
    
    <tr id="policyheader">
        <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">#</th>
        <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">Classe</th>
        <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">Age Range</th>                        
        <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">NSSF</th>
        <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">Couverture Ambulatoire</th>
        <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">Couverture M&eacute;dicament</th>
        <td id="policytd" style="font-size: 10pt; line-height:11pt;  font-family:tahoma;">Couverture Visites M&eacute;dicales                                                
        </th>
    </tr>
        
    <?
                $query_age="select id,range from age_range order by id";
                $query_class="select id,typec from class_type order by id";
                
                for($counter=1;$counter<=$nb_family_id;$counter++){
                    $ar=$_POST["class_type_id"][$counter];
                    $query_class="select id,typec from class_type order by id";
                    if($_POST["class_type_id"][$counter])$query_age="select distinct a.id,a.range 
                                                            from age_range as a
                                                            inner join policy as p
                                                                on p.age_range_id = a.id
                                                            inner join class_type as c
                                                                on c.id = p.class_type_id
                                                            where p.class_type_id = $ar
                                                                  and p.family_type_id = $nb_family_id          
                                                            order by a.id";
                    
                    else $query_age="select 1=1";
                    //echo $query_age;
                    echo "
                        
                        <input type=\"hidden\" value=\"$nb_family_id\" name=\"family_nb\"/>    
                        <input type=\"hidden\" value=\"$nb_family_id\" name=\"nb_family_id\"/>            
                        <TR>
                            <TD id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".$counter."</TD>
                            <TD id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>";comboBox($query_class,"class_type_id[$counter]",$_POST["class_type_id"][$counter],true,"onChange='AjaxFunction(this.value,$counter);'"); echo "</TD>
                            <TD><div id=\"age_range_id[$counter]\" name =\"age_range_id[$counter]\" >";comboBox($query_age,"age_range_id[$counter]",$_POST["age_range_id"][$counter],true); echo "</div></TD>
                            <TD><center><input type='checkbox' name='nssf[$counter]'/></center></TD>
                            <TD><center><input type='checkbox' name='amb[$counter]'/></center></TD>
                            <TD><center><input type='checkbox' name='med[$counter]'/></center></TD>
                            <TD><center><input type='checkbox' name='doc[$counter]'/></center></TD>
                        </TR>
                        <input type=\"hidden\" value=\"true\" name=\"counter[$counter]\"/>
                        
                        ";
 
                }
            ?>
            </table>
            <?
    
                echo "<br><br>
                
                <input type=\"submit\" value=\"&Eacute;tape suivante\" name=\"sbm\"/>
                <input type=\"hidden\" value=\"2\" name=\"step\"/>
                
                </center><br><br><br><br><br><br><br><br><br><br><br><br>
                </form>
                
                ";
    
    }
    ?>
    
    </html>
Drugoholic
Forum Newbie
Posts: 2
Joined: Thu Jul 30, 2009 3:17 am

Re: Help with Dynamic Forms

Post by Drugoholic »

When you goto the url it'll ask for a user/pass

user: axa
pass: guest

Thank you again.
Post Reply