本文介绍了动态HTML和PHP表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在页面内显示表单的项目。这个表单有一个能力.Ability是一个添加行按钮,用户可以增加表单行的数量(尝试下面的代码来查看将发生的事情,看看img)。

在此处输入图片说明



https://i.stack.imgur.com/AIFid.png



服务表(以html格式显示):



| ------------- - | -------------- |

| id |标题|

| -------------- | -------------- |

| 1 | service1 |

| -------------- | -------------- |

| 2 | service2 |

| -------------- | -------------- |

| 3 | service3 |

| -------------- | -------------- |

| 4 | service4 |

| -------------- | -------------- |

| 5 | service5 |

单位表:



| -------------- | ----- --------- | ----------------- |

| id |标题| bacicPrice |

| -------------- | -------------- | --------- -------- |



Unit_Service表(用于加入服务表和单位表):



| -------------- | ------------------- | ----------- ------ |

| id | service_id | unit_id |

| -------------- | ------------------- | ---- ------------- |



服务从数据库的服务表中获取。当用户点击sumbit title和基本价格插入单位使用for命令插入多个输入的表。然后使用for命令在unit_service表上插入服务id和unit id。

问题是当我的代码想在unit_service表上添加服务id和unit id时已选中的复选框将为unit_service中的每个单元添加。



我尝试过的方法:



php:



I am developing a project that show a form inside a page.This form have a ability.Ability is a add row button that user can increase number of form row (Try following codes to look what will happened look following img).
enter image description here

https://i.stack.imgur.com/AIFid.png

Services table(for show in form in html) :

|--------------|--------------|
| id | title |
|--------------|--------------|
| 1 | service1 |
|--------------|--------------|
| 2 | service2 |
|--------------|--------------|
| 3 | service3 |
|--------------|--------------|
| 4 | service4 |
|--------------|--------------|
| 5 | service5 |
Unit table :

|--------------|--------------|-----------------|
| id | title | bacicPrice |
|--------------|--------------|-----------------|

Unit_Service table(for join service table and unit table) :

|--------------|-------------------|-----------------|
| id | service_id | unit_id |
|--------------|-------------------|-----------------|

Services are taken from services table from database.When user click sumbit title and basic price inserted on unit table with a for command to insert several inputs.Then services id and unit id insert on unit_service table with for command.
The problem is when my codes wants to add service id and unit id on unit_service table any checkbox that checked will add for every unit in unit_service.

What I have tried:

php :

 <?php
if (isset($_POST['submit'])) {
    include_once "../db.php";

    $unit_title = $_POST['unit_title'];
    $unit_basic_price = $_POST['unit_basic_price'];
    $unit_checkbox = $_POST['checkbox'];

    for ($i = 0; $i < count($unit_title); $i++) {

        $sql_insert_boomgardi_unit = "insert into unit (title,basicPrice) values('$unit_title[$i]','$unit_basic_price[$i]')";

        if ($connection->query($sql_insert_boomgardi_unit) === true) {

            $last_id_unit = mysqli_insert_id($connection);

            for ($k = 0; $k < count($unit_checkbox); $k++) {

                $sql_insert_boomgardi_unit_service = "insert into unit_service (service_id, unit_id)
                        values('$unit_checkbox[$k]','$last_id_unit')";

                if ($connection->query($sql_insert_boomgardi_unit_service) === true) {
                    $unit_status = "OK";
                }
            }
        }
    }
    $connection->close();
}










<form method="post">
    <table id="unit">
        <tr id="row1">
            <td>
                <input placeholder='Title' type='text' name='unit_title[]'>
                <input placeholder='Basic price' type='text' name='unit_basic_price[]'>
                <div>
                    <?php
                    include_once '../db.php';
                    $sql = mysqli_query($connection, 'SELECT * FROM services');
                    while ($row = $sql->fetch_assoc()) {
                        echo "<label><input name='checkbox[]' value='$row[id]' type='checkbox'>$row[title]</label>";
                    }
                    ?>
                </div>
            </td>
        </tr>
    </table>
    <button type="button" onclick="add_row()">Add room</button>
    <input value="Submit" type="submit" name="submit"/>
</form>

<script type="text/javascript">
    function add_row() {
        $rowno = $("#unit tr").length;
        $rowno = $rowno + 1;
        $phpcode = "<?php $sql = mysqli_query($connection, 'SELECT * FROM services'); while ($row = $sql->fetch_assoc()) {
            echo " <label><input name='checkbox[]' value='$row[id]' type='checkbox'>$row[title]</label> ";
        } ?>";
        $("#unit tr:last").after("<hr/><tr id='row" + $rowno + "'><td><input placeholder='Title' type='text' name='unit_title[]'><input placeholder='Basic price' type='text' name='unit_basic_price[]'><div>" + $phpcode + "</div></td> </tr>");
    }
</script>

推荐答案




这篇关于动态HTML和PHP表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:54