将其他行添加到数据库时如何插入其他信息

时间:2015-07-29 04:07:49

标签: javascript php mysql database

我的脚本可以在点击时添加其他行。但是,我只能将第一行的内容存储在数据库中。如何启用其他附加行的内容也存储在数据库中?

<script>
        function addRowToTable()
        {
            var tbl = document.getElementById('tblSample');
            var lastRow = tbl.rows.length;
            var iteration = lastRow;
            var row = tbl.insertRow(lastRow);

            // left cell
            var cellLeft = row.insertCell(0);
            var textNode = document.createTextNode(iteration);
            cellLeft.appendChild(textNode);

            // right cell
            var cellRight = row.insertCell(1);
            var el = document.createElement('input');
            el.type = 'text';
            el.name = 'time[]';
            el.id = 'txtRow' + iteration;
            el.size = 40;

            el.onkeypress = keyPressTest;
            cellRight.appendChild(el);

            // select cell
            var cellRightSel = row.insertCell(2);
            var sel = document.createElement('select');
            sel.name = 'date[]';
            sel.options[0] = new Option('Select a day', '');
            sel.options[1] = new Option('Monday', 'Monday');
            sel.options[2] = new Option('Tuesday', 'Tuesday');
            sel.options[3] = new Option('Wednesday', 'Wednesday');
            sel.options[4] = new Option('Thursday', 'Thursday');
            sel.options[5] = new Option('Friday', 'Friday');
            sel.options[6] = new Option('Saturday', 'Saturday');
            sel.options[7] = new Option('Sunday', 'Sunday');
            cellRightSel.appendChild(sel);
        }
</script>

这是我的HTML代码的一部分:

<table border="1" id="tblSample" class="table table-bordered">
   <tr>
     <th colspan="5">Add Day and time
        <input type="button" class="btn-danger addright" value="Add" onclick="addRowToTable();" /> 
        <input type="button" class="btn-danger removeright" value="Remove" onclick="removeRowFromTable();" />
     </th>
    </tr>
    <tr>
   <td>1</td>
      <td><input type="text" name="time[]" id="txtRow1" size="40" placeholder="e.g 8:00am - 3:00pm" onkeypress="keyPressTest(event, this);" />   </td>
      <td>
      <select name="date[]">
       <option value="">Select a day</option>
       </select>
       </td>
      </tr>
 </table>

1 个答案:

答案 0 :(得分:2)

                $timeArr = $_POST['time'];
                $dateArr = $_POST['date'];
                //count the array
                for ($i = 0; $i < count($timeArr); $i++) {
                    $datetimeoption = new datetimeoption();
                    $datetimeoption->prefer_day = $dateArr[$i];
                    $datetimeoption->prefer_time = $timeArr[$i];
                    //Insert data into database
                    $result = $datetimeoption->createdatetime();
                }