简体   繁体   English

意外的标识符,而

[英]Unexpected identifier while

I have an unexpected code error but I can't identify it.我有一个意外的代码错误,但我无法识别它。

Code:代码:

$('#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>';
  });

Within my role this echo line is giving unexpected error:在我的角色中,这条回声线给出了意外错误:

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

Full code:完整代码:

$(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>';
  });


});

Right here on this line you have an opening PHP tag without closing the table cell and line of code:就在这一行,您有一个打开的 PHP 标记,但没有关闭表格单元格和代码行:

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

EDIT You want this, here is the whole block:编辑你想要这个,这是整个块:

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

BUT YOU HAVE AN ISSUE HERE You're trying to run PHP inside of a JavaScript command and that will not work.但是您在这里遇到了一个问题您正在尝试在 JavaScript 命令中运行 PHP,但这是行不通的。 What you need to do, if you want to keep your function "as is", is to trigger an AJAX request to your PHP and return the information to be placed into the options in your markup.如果您想保持函数“原样”,您需要做的是触发对 PHP 的 AJAX 请求,并返回要放入标记选项中的信息。 Therefore your code should be something like this:因此你的代码应该是这样的:

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

With options.php being something like this: 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>";
       }
    ?>

BUT YOU MAY RUN INTO A TIMING ISSUE because #data5 may not exist when the AJAX returns the data from the PHP, so you must make sure #data5 exists before the AJAX call is made.但是您可能#data5问题,因为当 AJAX 从 PHP 返回数据时, #data5可能不存在,因此您必须在进行AJAX 调用之前确保#data5存在。

You broke into multiple lines without escaping the new lines with '\\'.您闯入多行而没有用“\\”转义新行。

So, if you want to add the php code into html variable.所以,如果你想将 php 代码添加到html变量中。 then, you can try this,,那你可以试试这个,

Therefore, You can try adding '\\',因此,您可以尝试添加“\\”,

$('#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>';
  });

or, I would recommend this,或者,我会推荐这个,

$('#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>';
  });



But, if you want execute the PHP code then check your server, because Its probably not working.但是,如果您想执行 PHP 代码,请检查您的服务器,因为它可能无法正常工作。 If it did then you should not get PHP code in the browser.如果是这样,那么您不应在浏览器中获取 PHP 代码。

Else please describe your problem more precisely,否则请更准确地描述您的问题,

  • Do you want to exec the PHP code??你要执行PHP代码吗??
  • Or, it is already executed??或者,它已经被执行了??
  • Or, Do you want to show the PHP code in your html??或者,你想在你的 html 中显示 PHP 代码吗?

First I suggest you to add parentheses around attribute value:首先,我建议您在属性值周围添加括号:

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

And it will be much more helpful, if you have said what the error do you have?如果你说你有什么错误,它会更有帮助? You can always see html source code generated by the php script.您始终可以看到由 php 脚本生成的 html 源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM