使用js clone在1个字段中插入多个复选框值,但它会重复值

时间:2018-08-09 09:02:51

标签: javascript php mysqli clone

我制作了一个PHP表单,使用克隆表单代码在MySQL中插入数据。除了复制所有复选框并将其全部插入我克隆的每个新表格中之外,其他所有内容都正常运行。

例如 我插入时

(f.name, l.name, date, tours)
(A,         B,   TODAY, T1)
(C,         D,   TODAY, T2)

结果是

(f.name, l.name, date,  tours)
(A,         B,   TODAY, T1,t2)
(C,         D,   TODAY, T1,t2)

表格代码

    <?php
//including the database connection file
include_once("../config/config.php");

$resulttours = mysqli_query($mysqli, "SELECT toursname FROM tours ORDER BY id     DESC"); // using mysqli_query instead

?>

<html>
<head>
    <title>Add Room Category</title>

    <script     src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js">    </script>
    <script     src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script>

    $(document).ready(function(){
        $( ".add-row" ).click(function(){
            var $clone = $( "ul.personal-details" ).first().clone();
            $clone.append( "<button type='button' class='remove-row'>-</button>" );
            $clone.insertBefore( ".add-row" );
        });

        $( ".form-style-9" ).on("click", ".remove-row", function(){
            $(this).parent().remove();
        });
    });

    function getrows()
    {
        var rows = document.getElementsByTagName('tr');
        numrows = rows.length; 
    }

    </script>
</head>

<body>
    <a href="insertmultiformindex.php">Home</a>
    <br/><br/>
    <form action="insertmultiformadd.php" method="post" name="form_roomcat">
        <ul class="personal-details">
            <table width="25%" border="0">
                <tr> 
                    <td>Room Category</td>
                    <td><input type="text"  name="xxx1[]"></td>
                    <td><input type="text"  name="xxx2[]"></td>
                    <td><input type="date"  name="date1[]"  ></td>
                    <td>
                        <?php 
                        //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is     deprecated, we need to use mysqli_fetch_array 
                        while($res = mysqli_fetch_array($resulttours)) {         
                            //echo "</br>";
                            echo $toursname = "".$res['toursname']."";
                            echo "<input type='checkbox' name='toursname[]'     id='".$res['toursname']."'   value='".$res['toursname']."' /><br>";
                        }
                        ?>
                    </td>
                </tr>
            </table>
        </ul>
        <td><button type="button" class="add-row">+</button> </td>
        <td><input type="submit" name="Submit" value="Add"></td>
    </form>
</body>
</html>

这是操作页面代码

<html>
<head>
    <title>Add Data</title>
</head>

<body>

<a href="insertmultiform.php">Add New </a><br/>
<a href="insertmultiformindex.php">index </a><br/><br/>

<?php
//including the database connection file
include_once("../config/config.php");

if(isset($_POST['Submit'])) {
    $xxx1 = $_POST['xxx1'];
    $xxx2 = $_POST['xxx2'];
    $date1 = date('Y-m-d', strtotime($_POST['date1']));
    $checkbox1 = $_POST['toursname'];
    $chk=""; 

        foreach($checkbox1 as $chk1) 
        { 
        $chk.= $chk1.","; 
        }    


for ($i = 0; $i < count($_POST['xxx1']); $i++) {
    $xxx1 = $_POST['xxx1'][$i];
    $xxx2 = $_POST['xxx2'][$i];
    $date1 = date('Y-m-d', strtotime($_POST['date1'][$i]));
    $checkbox1=$_POST['toursname'][$i];  

//here, inside the loop, run your database query using the 3 values above   

$result = mysqli_query($mysqli, "INSERT INTO 
        test (`value1`,`value2`,`date1`,`value3`) 
        VALUES('$xxx1','$xxx2' ,'$date1','$chk')"); 
}
}

1 个答案:

答案 0 :(得分:0)

最好的方法是使用json_encode()。

$xxx1 = json_encode($_POST['xxx1']);
$xxx2 = json_encode($_POST['xxx2']);
$date1 = json_encode(date('Y-m-d', strtotime($_POST['date1'])));
$result = mysqli_query($mysqli, "INSERT INTO 
          test (`value1`,`value2`,`date1`) 
          VALUES('$xxx1','$xxx2' ,'$date1')");

当需要的数据显示时,请使用json_decode();

示例:json_decode($res['value1'])

并使用foreach:

foreach (json_decode($res['value1']) as $key =>$value) {...}