2017-12-27 1000 views
0

我有一個HTML表格行,其中包含根據需要自定義的混合內容單元格。我想將此行動態添加到HTML表格。但是js中出現了一個錯誤。將自定義表格行添加到PHP中的HTML表格中

php的功能如下

function addRowtoTable(){ 
    $tblRow = '<tr> 
       <td><input type="checkbox" name="record"></td>  
       <td for="section"><select name=\'section\' id=\'section\' class=\'form-control\' >  
          <?php echo fill_sectionName($conn, $userRow[\'userName\']); ?></select></td> 
       <td for="fileRef"><input name ="fileRef" id="fileRef" class="form-control ui-autocomplete-input" autocomplete="on" placeholder="Enter File Reference"></td> 
       <td for="datepicker"><input class=\'form-control\' type="text" id="datepicker" placeholder="Mail Date"></td>     
       <td for="section"><?php echo fill_SecurityClass(); ?></td>  
       <td for="section"><?php echo fill_Precedence(); ?></td> 
       <td for="section"><?php echo fill_AppointmentList($conn); ?></td> 
       <td for="section"><input name ="address1" id="address1" class="form-control ui-autocomplete-input" autocomplete="on" placeholder="Enter Stn Name"></td> 
       <td for="section"><?php echo fill_ModeOfDespatch(); ?></td> 
      </tr> 
        '; 
    return ($tblRow); 

請注意,我調用了HTML代碼中的PHP函數用於填充自動完成,日期選擇器,並選擇對象。

的JS腳本添加到我的表中的這個<tr>如下:

$('#addtblRow').click(function() { 
    var tblRowNew = <?php echo addRowtoTable()?> ; 
    $("table tbody").append(tblRowNew); 
    }); 

瀏覽器的錯誤消息的截圖是這樣

screenshot of the error message

我能得到幫助解決這個錯誤。

回答

0

我做了不同的方法。在PHP中我聲明的變量

<?php 
    global $sectionNames; 
    global $Prece; 
    global $AddeeAppt; 
    global $ModesOfDes; 

    $sectionNames = fill_sectionName($conn, $userRow["userName"]); 
    $Prece = fill_Precedence(); 
    $AddeeAppt = fill_AppointmentList($conn); 
    $ModesOfDes = fill_ModeOfDespatch($conn); 
?> 

$("#addtblRow").click(function(){ 
     var numofrows = $("#num_rows").val(); 
     //window.alert(numofrows); 
     for(var i=0;i<numofrows ;i++){ 
     addNewRow(); 
     } 
    }); 

的JS修改這樣

function addNewRow(){ 
    // 
    var trd=""; 
    trd +="<tr>"; 
    trd +="<td><input type='checkbox' name='record' class='form-control' ></td>"; 
    trd +="<td for='section'><select name='section' id='section' class='form-control'><?php echo $sectionNames ?> </select></td>"; 
    trd +="<td><input name='fileRef' class='form-control'></td>"; 
    trd +="<td for='datepicker'><input class='form-control' type='text' id='datepicker' placeholder='Mail Date'></td>"; 
    trd +="<td><?php echo $Prece ?></td>"; 
    trd +="<td><?php echo $AddeeAppt ?></td>"; 
    trd +="<td><input name='Address' class='form-control'></td>"; 
    trd +="<td><?php echo $ModesOfDes ?></td>"; 
    trd +="</tr>"; 
    $("table tbody").append(trd); 
} 

和代碼工作正常,我。 PS:JS不允許在其var變量中返回。所以所有的HTML元素/標籤都是連續的。