动态HTML表单并保存到数据库的多个记录中

时间:2019-04-15 09:44:28

标签: php codeigniter

我正在使用codeigniter编写一个小型应用程序来跟踪付款和付款日期。我有一个表格,我们在其中输入有关客户的详细信息,在这些字段中,我们输入将用于支付客户全部保险费的分期付款。

下一页将使用此数字,并生成相同数量的字段。例如instalment_1,date_1; instalment_2,date_2。

<?php for($i=1; $i<=$instalments; $i++):?>
    <div class="row form-group">
        <div><label>Instalment <?php echo $i;?></label></div>
        <div>
            <input type="text" id="instalment_<?php echo $i;?>" name="instalment_<?php echo $i;?>">
    </div>
</div>

<div class="row form-group">
    <div><label>Premium Date <?php echo $i;?></label></div>
    <div>
        <input type="text" id="date_<?php echo $i;?>" name="date_<?php echo $i;?>" class="form-control">
    </div>
</div>

现在的想法是循环浏览此表单,并将每期付款和日期集保存到数据库的1条记录中。 如果我只有一种类型的字段(例如,分期付款),我就能做到。但是当我添加日期时,它会保存,但是每个记录都会创建两次。

$post = $this->input->post();   
foreach($post as $key=>$value){
            //check for the word instalment
            if(strpos($key, "instalment") == 0){
                //get the instalment number from the name of the field
                $inst_number = substr($key, -1);
                //Use $category_id and $value in this loop to build update statement
                echo "<script type='text/javascript'>alert('$inst_number');</script>";
                //$arr = "inst_".$inst_number;
                //reform the name of the field to get the values from it
                $instalment_var = "instalment_".$inst_number;
                $date_var = "date_".$inst_number;
                echo "<script type='text/javascript'>alert('$instalment_var');</script>";
                $amount = $this->input->post($instalment_var);
                $date = $this->input->post($date_var);
                echo "<script type='text/javascript'>alert('$amount');</script>";
                $arr = array(
                    'payment_amount' => $amount,
                    'payment_date' => $date,
                    'policy_name' => $policy_id,
                );
                if(!$this->Policy_model->save_payment_details($arr)){
                    redirect('ships/index');
                }
            }
        }

我该怎么做,以免每次记录都保存两次?如果我添加另一个字段,它将保存3次。

我虽然要命名div并进行处理,但是我没有得到任何结果。

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试此代码。这将解决您的问题。

查看:

<section id="owl-banner" class="owl-carousel owl-theme">
        <div class="item">
            <img src="img/banner/banner-2.jpg" alt="banner image 2">
            <div class="homebannercontent">
                <h5 style="color:#ffffff;font-size:60px;font-family:Titillium Web;line-height:1.6;">Truhealing-</h5>
                <p style="color:#ffffff; font-size:30px;font-family:Titillium Web;line-height:1.6;">Complete Health care <br>Solution for women</p>
            </div>
        </div>
        <div class="item">
            <img src="img/banner/banner-5.jpg" alt="banner image 4">
            <div class="homebannercontent">
                <h5 style="color:#ffffff;font-size:60px;font-family:Titillium Web;line-height:1.6;">Your health is in <br>your hands</h5>
            </div>
        </div>
        <div class="item">
            <img src="img/banner/banner-3.jpg" alt="banner image 3">
            <div class="homebannercontent">
                <h5 style="color:#ffffff;font-size:60px;font-family:Titillium Web;line-height:1.6;">Get your hormones <br>working for you</h5>
            </div>
        </div>
        <div class="item">
            <img src="img/banner/banner-4.jpg" alt="banner image 4">
            <div class="homebannercontent">
                <h5 style="color:#ffffff;font-size:60px;font-family:Titillium Web;line-height:1.6;">Address <br>the root cause</h5> 
            </div>
        </div>
        <div class="col-md-6">
                    <form action="/action_page.php">
                        <div class="form-group">
                            <label for="email">Email:</label>
                            <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
                        </div>
                        <div class="form-group">
                            <label for="pwd">Password:</label>
                            <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
                        </div>
                        <button type="submit" class="btn btn-default">Submit</button>
                    </form>
                </div>
    </section>

在您的控制器中:

<form action="action.php" method="POST">
<?php for($i=1; $i<=$instalments; $i++){?>
    <div class="row form-group">
        <div><label>Instalment <?php echo $i;?></label></div>
        <div>
            <input type="text" id="instalment[]" name="instalment[]">
    </div>
</div>

<div class="row form-group">
    <div><label>Premium Date <?php echo $i;?></label></div>
    <div>
        <input type="text" id="date_<?php echo $i;?>" name="date[]" class="form-control">
    </div>
</div>
<?php } ?>
<input type="submit" name="submit" value="submit">
</form>