簡體   English   中英

意外的標識符,而

[英]Unexpected identifier while

我有一個意外的代碼錯誤,但我無法識別它。

代碼:

$('#add1').click(function(){
   var html = '<tr>';
   html += '<td contenteditable ><input type="date" class="input-date" id="data1"/></td>';
   html += '<td contenteditable><input type="text" class="input-day" id="data2"/></td>';
   html += '<td contenteditable ><input type="time" id="data3"/></td>';
   html += '<td contenteditable><input type="time"  id="data4"/></td>';
   html += '<td contenteditable><select id="data5"><option></option><?php
   $sql = "SELECT nome FROM raddb.Utente ORDER BY nome ASC";
   $qr = mysqli_query($conn, $sql);
   while($ln = mysqli_fetch_assoc($qr)){
   echo "<option value=".$ln['nome'].">".$ln['nome']."</option>";
   }?></select></td>';
   html += '</tr>';
  });

在我的角色中,這條回聲線給出了意外錯誤:

echo "<option value=".$ln['nome'].">".$ln['nome']."</option>";

完整代碼:

$(document).ready(function(){
  dom: 'Bflrtip',
  fetch_data();

  function fetch_data()
  {
   var dataTable = $('#user_data').DataTable({
       dom: 'Bflrtip',
    "processing" : true,
    "serverSide" : true,
    "pagingType": "full_numbers",
        "iDisplayLength": 5,
        "oLanguage": {
    "sProcessing": "Aguarde enquanto os dados são carregados ...",
    "sLengthMenu": "Mostrar _MENU_ registos por página",
    "sZeroRecords": "Nenhum registo correspondente ao criterio encontrado",
    "sInfoEmtpy": "Exibindo 0 a 0 de 0 registos",
    "sInfo": "Exibindo de _START_ a _END_ de _TOTAL_ registos",
    "sInfoFiltered": "",
    "sSearch": "<span class='glyphicon glyphicon-search'></span>",
    "oPaginate": {
       "sFirst":    "<span class='glyphicon glyphicon-fast-backward'></span>",
       "sPrevious": "<span class='glyphicon glyphicon-backward'></span>",
       "sNext":     "<span class='glyphicon glyphicon-forward'></span>",
       "sLast":     "<span class='glyphicon glyphicon-fast-forward'></span>"
     }
    },
    buttons: [
      {
            extend: 'excel',
                text: 'excel',
                title: 'Consultas Semanais',

        },
        {
            extend: 'pdf',
                text: 'pdf',
                title: 'Consultas Semanais',

        },
        {
            extend: 'print',
                text: 'print',
                title: 'Consultas Semanais',
                customize: function ( win ) {
                    $(win.document.body)
                        .css( 'font-size', '10pt' );

                    $(win.document.body).find( 'table' )
                        .addClass( 'compact' )
                        .css( 'font-size', 'inherit' );
                }
        }
    ], 
    "order" : [],
    "ajax" : {
     url:"./fetchconsulta",
     type:"POST"
    }   
    });   
    }

    function update_data(id, column_name, value)
   {
   $.ajax({
    url:"./updateconsulta",
    method:"POST",
    data:{id:id, column_name:column_name, value:value},
    success:function(data)
    {
     $('#alert_message').html('<div class="alert alert-success">'+data+'</div>');
    }
   });
   setInterval(function(){
    $('#alert_message').html('');
   }, 5000);
  }

  $(document).on('blur', '.update', function(){
   var id = $(this).data("id");
   var column_name = $(this).data("column");
   var value = $(this).text();
   update_data(id, column_name, value);
  });

  var semana = ["Domingo", "Segunda-Feira", "Terça-Feira", "Quarta-Feira", "Quinta-Feira", "Sexta-Feira", "Sábado"];

   $('#add1').click(function(){
   var html = '<tr>';
   html += '<td contenteditable ><input type="date" class="input-date" id="data1"/></td>';
   html += '<td contenteditable><input type="text" class="input-day" id="data2"/></td>';
   html += '<td contenteditable ><input type="time" id="data3"/></td>';
   html += '<td contenteditable><input type="time"  id="data4"/></td>';
   html += '<td contenteditable><select id="data5"><option></option><?php
   $sql = "SELECT nome FROM raddb.Utente ORDER BY nome ASC";
   $qr = mysqli_query($conn, $sql);
   while($ln = mysqli_fetch_assoc($qr)){
   echo "<option value=".$ln['nome'].">".$ln['nome']."</option>";
   }?></select></td>';
   html += '</tr>';
  });


});

就在這一行,您有一個打開的 PHP 標記,但沒有關閉表格單元格和代碼行:

html += '<td contenteditable><select id="data5"><option></option><?php

編輯你想要這個,這是整個塊:

   html += '<td contenteditable><select id="data5"><option></option>';
   <?php
       $sql = "SELECT nome FROM raddb.Utente ORDER BY nome ASC";
       $qr = mysqli_query($conn, $sql);
       while($ln = mysqli_fetch_assoc($qr)){
          echo "<option value=".$ln['nome'].">".$ln['nome']."</option>";
       }
    ?>
   html += '</select></td>';
   html += '</tr>';

但是您在這里遇到了一個問題您正在嘗試在 JavaScript 命令中運行 PHP,但這是行不通的。 如果您想保持函數“原樣”,您需要做的是觸發對 PHP 的 AJAX 請求,並返回要放入標記選項中的信息。 因此你的代碼應該是這樣的:

html += '<td contenteditable><select id="data5"><option></option>';
$('#data5').load('options.php');
html += '</select></td>';
html += '</tr>';

options.php 是這樣的:

<?php
    $conn = mysqli_connect("HOST", "USER", "PW", "DATABASE");
    $sql = "SELECT nome FROM raddb.Utente ORDER BY nome ASC";
    $qr = mysqli_query($conn, $sql);
       while($ln = mysqli_fetch_assoc($qr)){
          echo "<option value=".$ln['nome'].">".$ln['nome']."</option>";
       }
    ?>

但是您可能#data5問題,因為當 AJAX 從 PHP 返回數據時, #data5可能不存在,因此您必須在進行AJAX 調用之前確保#data5存在。

您闖入多行而沒有用“\\”轉義新行。

所以,如果你想將 php 代碼添加到html變量中。 那你可以試試這個,

因此,您可以嘗試添加“\\”,

$('#add1').click(function(){
   var html = '<tr>';
   html += '<td contenteditable ><input type="date" class="input-date" id="data1"/></td>';
   html += '<td contenteditable><input type="text" class="input-day" id="data2"/></td>';
   html += '<td contenteditable ><input type="time" id="data3"/></td>';
   html += '<td contenteditable><input type="time"  id="data4"/></td>';
   html += '<td contenteditable><select id="data5"><option></option><?php \
   $sql = "SELECT nome FROM raddb.Utente ORDER BY nome ASC"; \
   $qr = mysqli_query($conn, $sql); \
   while($ln = mysqli_fetch_assoc($qr)){ \
   echo "<option value=".$ln[\'nome\'].">".$ln[\'nome\']."</option>"; \
   }?></select></td>';
   html += '</tr>';
  });

或者,我會推薦這個,

$('#add1').click(function(){
   var html = '<tr>';
   html += '<td contenteditable ><input type="date" class="input-date" id="data1"/></td>';
   html += '<td contenteditable><input type="text" class="input-day" id="data2"/></td>';
   html += '<td contenteditable ><input type="time" id="data3"/></td>';
   html += '<td contenteditable><input type="time"  id="data4"/></td>';
   html += `<td contenteditable><select id="data5"><option></option><?php
   $sql = "SELECT nome FROM raddb.Utente ORDER BY nome ASC";
   $qr = mysqli_query($conn, $sql);
   while($ln = mysqli_fetch_assoc($qr)){
   echo "<option value=".$ln['nome'].">".$ln['nome']."</option>";
   }?></select></td>`;
   html += '</tr>';
  });



但是,如果您想執行 PHP 代碼,請檢查您的服務器,因為它可能無法正常工作。 如果是這樣,那么您不應在瀏覽器中獲取 PHP 代碼。

否則請更准確地描述您的問題,

  • 你要執行PHP代碼嗎??
  • 或者,它已經被執行了??
  • 或者,你想在你的 html 中顯示 PHP 代碼嗎?

首先,我建議您在屬性值周圍添加括號:

echo "<option value=\"".$ln['nome']."\">".$ln['nome']."</option>";

如果你說你有什么錯誤,它會更有幫助? 您始終可以看到由 php 腳本生成的 html 源代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM