Add custom table row to HTML Table in PHP

html row add

25 观看


74 作者的声誉

I have an HTML table row with cells of mixed content customised as per necessity. I want to add this row dynamically to the HTML Table. But an error is popping up in the js.

the php function is as follows

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>   
    return ($tblRow);

Please note that I am calling php functions within the HTML Code for filling autocomplete, datepicker and select objects.

The JS script to add the this <tr> into my table is as follows:

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

The screenshot of the error message of the browser is like this

screenshot of the error message

Can I get help resolve this bug.

作者: Raky 的来源 发布者: 2017 年 12 月 27 日

回应 (1)


74 作者的声誉


I have made different approach. In php i declared variables

    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);

      var numofrows = $("#num_rows").val();
      for(var i=0;i<numofrows ;i++){

The JS was modified like this

   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); 

And the code works fine for me. PS: The JS Doesn't allow carriage return in its var variables. So all HTML Elements/Tags are to be contiguous.

作者: Raky 发布者: 28.12.2017 09:43