2017-07-27 4 views
-7

Ich möchte zum Einfügen mehrerer Zeilen von Daten an die database.I eine Tabelle erstellen haben ein Feld, wo ich eine Nummer eingeben, und klicken Sie auf „Hinzufügen“. Anzahl der Datensätze wird der Tabelle hinzugefügt.Wie man Listenfeld dynamisch auf Knopfdruck in PHP einfügt?

kann ich Anzahl der Datensätze hinzufügen, nun auf der Grundlage der rowcount eingefügt. Aber ich möchte Floor_id Feld sequenzielle numer sein, während es nur Copyig erstes Feld ist.

Kann jemand guilde wie erstes Feld als inkrementale Zahl zu erhalten.

Code Snippet

function addRow(tableID,num) 
 
\t \t { \t 
 
\t \t \t 
 
\t \t \t var num = $("#rownum").val(); 
 
\t \t \t $("#dataTable tr").slice(2).remove(); 
 
\t \t \t for (var n = 2; n <= num; n++) 
 
\t \t \t { 
 
\t \t \t \t \t var table = document.getElementById(tableID); 
 
\t \t \t \t \t var rowCount = table.rows.length; 
 
\t \t \t \t \t var row = table.insertRow(rowCount); 
 
\t \t \t \t \t var colCount = table.rows[0].cells.length; 
 
\t \t \t \t \t for(var i=0; i<colCount; i++) 
 
\t \t \t \t \t { 
 
\t \t \t \t \t \t var newcell \t = row.insertCell(i); 
 
\t \t \t \t \t \t newcell.innerHTML = table.rows[1].cells[i].innerHTML; 
 
\t \t \t \t \t \t //alert(newcell.childNodes); 
 
\t \t \t \t \t \t switch(newcell.childNodes[1].type) 
 
\t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t case "text": 
 
\t \t \t \t \t \t \t \t \t newcell.childNodes[1].value = ""; 
 
\t \t \t \t \t \t \t \t \t break; 
 
\t \t \t \t \t \t \t case "select-one": 
 
\t \t \t \t \t \t \t \t \t newcell.childNodes[1].selectedIndex = 0; 
 
\t \t \t \t \t \t \t \t \t break; 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t } 
 
\t \t \t } 
 
\t \t }
<HTML> 
 
<HEAD> 
 
\t <TITLE> Add/Remove dynamic rows in HTML table </TITLE> 
 
\t <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
</HEAD> 
 
<BODY> 
 
<div class="results"></div> 
 
\t \t 
 
\t \t <fieldset style="width: 1600px;"> 
 
\t \t \t <legend><b>Building Layout Info</b></legend> 
 
\t \t \t <TABLE id="dataTable" width="350px" border="1"> 
 
\t \t \t <tr> 
 
\t \t \t \t <th>Floor_Id</th> 
 
\t \t \t \t <th>Front_F_Mark </th> 
 
\t \t \t \t <th>Rear_F_Mark</th> 
 
\t \t \t \t <th>Floor_Type</th> 
 
\t \t \t </tr> 
 
\t \t \t <TR> 
 
\t \t \t \t <td><input type= "text" name= "Floor_Id[]" value="1" /> </td> 
 
\t \t \t \t <td><input type= "text" name= "Front_F_Mark[]" /> </td> 
 
\t \t \t \t <td><input type= "text" name= "Rear_F_Mark[]" /> </td></td> 
 
\t \t \t \t <td><select name="Floor_Type[]" id="Floor_Type" > \t <br /> 
 
\t \t \t \t \t \t \t <option value="">Floor_Type </option> 
 
\t \t \t \t \t \t \t <option value="Normal">Normal</option> 
 
\t \t \t \t \t \t \t <option value="Hidden">Hidden</option> </select> 
 
\t \t \t \t \t \t </select> 
 
\t \t \t \t </td> 
 
\t \t \t </TR> 
 
<labe>Number of Records<input class="row" type='text' id='rownum' name='rownum' /> </label> 
 
\t <INPUT type="button" value="Add Row" onclick="addRow('dataTable');" /> 
 
</TABLE> 
 
</body> 
 

 
</html>

+0

Willkommen SO. Bitte besuchen Sie die ** [Hilfe-Center] (http://stackoverflow.com/tour) ** und nehmen Sie die Tour, um zu sehen, was und wie zu fragen. TIPP: Postleitzahlen und Bemühungen - –

+0

@bub danke für das Erwähnen. Dasselbe wurde aktualisiert. – N1209

+0

@ ItamarG3 können Ihnen helfen, wie List-Feld hinzuzufügen, als nur Textfelder hier das Hinzufügen .My Listenfeld Daten über PDO holen .. – N1209

Antwort

0

Ich konnte dies durch folgende Änderungen acheive.

<HTML> 
<HEAD> 
<TITLE> Add/Remove dynamic rows in HTML table </TITLE> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

</HEAD> 
<BODY> 
    <div class="results"></div> 

    <fieldset style="width: 1600px;"> 
     <legend><b>Building Layout Info</b></legend> 
     <TABLE id="dataTable" width="350px" border="1"> 
     <tr> 
      <th>Floor_Id</th> 
      <th>Front_F_Mark </th> 
      <th>Rear_F_Mark</th> 
      <th>Floor_Type</th> 
     </tr> 
     <TR> 
      <td><input type= "text" name= "Floor_Id[]" value="1" /> </td> 
      <td><input type= "text" name= "Front_F_Mark[]" /> </td> 
      <td><input type= "text" name= "Rear_F_Mark[]" /> </td></td> 
      <td><select name="Floor_Type[]" id="Floor_Type" > <br /> 
         <option value="">Floor_Type </option> 
         <option value="Normal">Normal</option> 
         <option value="Hidden">Hidden</option> </select> 
        </select> 
      </td> 
     </TR> 
     <labe>Number of Records<input class="row" type='text' id='rownum' name='rownum' /> </label> 
     <button type="button" class='loadfloor' style="float: right;"> Load Floors</button> 
     </TABLE> 
    </form> 
    <script> 
    $(".loadfloor").on('click',function(){ 
    var num = $("#rownum").val(); 
    $("#dataTable tr").slice(2).remove(); 
    for (var i = 2; i <= num; i++) 
    { 
     var data = "<tr><td><input type='text' id='Floor_Id"+i+"' name='Floor_Id[]' value = "+i+" /></td><td><input type='text' id='Front_F_Mark"+i+"' name='Front_F_Mark[]'/></td><td><input type='text' id='Rear_F_Mark"+i+"' name='Rear_F_Mark[]'/></td><td><select id='Floor_Type"+i+"' name='Floor_Type[]'><option value='null'>Floor_Type</option><option value='Normal'>Normal</option><option value='Hidden'>Hidden</option></td></tr>"; 
     $("#dataTable").append(data); 
    } 
}); 
    </script> 
</body> 
</html> 
Verwandte Themen