MySQL while循环中JavaScript和HTML标记中的变量

MySQL while循环中JavaScript和HTML标记中的变量,javascript,php,html,mysql,Javascript,Php,Html,Mysql,如何在PHP文件中获取Java脚本代码中的PHP变量?这段代码位于PHP while循环中,因此我知道我必须以某种方式从mysql结果中为每个JS属性分配$id,并将它们传递给html的输入,这样每个html标记都会彼此独立 以下是PHP文件代码: <script> src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> while ($row = mys

如何在PHP文件中获取Java脚本代码中的PHP变量?这段代码位于PHP while循环中,因此我知道我必须以某种方式从mysql结果中为每个JS属性分配$id,并将它们传递给html的输入,这样每个html标记都会彼此独立

以下是PHP文件代码:

<script> 
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

while ($row = mysql_fetch_array($query)) 
{
$id = $row['id'];

<select name='alarm_action' id="alarm_action" required>
    <option value="">Default Action</option>
    <option value='Play Sound'>Play Sound</option>
    <option value='Say This'>Say This</option>
    <option value='Run Command'>Run Command</option>
    <option value='Run Program'>Run Program</option></select>
<input name="file_path" id="file_path" type="text" style="display: none" required/>




<script>
var alarmInput = $('#alarm_action');
alarmInput.on('change', function() {
  var file_path = $('#file_path');
  file_path.show();
  switch ($(this).val()) {
    case 'Play Sound':
    case 'Run Program':
      file_path.attr('type', 'file');
      break;
    case 'Say This':
    case 'Run Command':
      file_path.attr('type', 'text');
      break;
    case '':
     file_path.hide();
    break;                      
  }
});
</script>

}

src=”https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
while($row=mysql\u fetch\u array($query))
{
$id=$row['id'];
默认动作
播放声音
说这个
运行命令
运行程序
var alarmInput=$(“#报警_操作”);
alarmInput.on('change',function()){
var file_path=$(“#file_path”);
文件_path.show();
开关($(this.val()){
案例“播放声音”:
案例“运行程序”:
file_path.attr('type','file');
打破
“说这个”案例:
案例“运行命令”:
文件_path.attr('type','text');
打破
案例“”:
file_path.hide();
打破
}
});
}
只需使用

    <?php 

$id = $row['id']; 

?>
<!-- html  -->
<p><?php  echo $id; ?></p>

添加javascript

<script>
//var id = <?php echo $id; ?>
var alarmInput = $('<?php echo $id; ?>');
alarmInput.on('change', function() {
  var file_path = $('#file_path');
  file_path.show();
  switch ($(this).val()) {
    case 'Play Sound':
    case 'Run Program':
      file_path.attr('type', 'file');
      break;
    case 'Say This':
    case 'Run Command':
      file_path.attr('type', 'text');
      break;
    case '':
     file_path.hide();
    break;                      
  }
});
</script>

//变量id=
变量alarmInput=$('');
alarmInput.on('change',function()){
var file_path=$(“#file_path”);
文件_path.show();
开关($(this.val()){
案例“播放声音”:
案例“运行程序”:
file_path.attr('type','file');
打破
“说这个”案例:
案例“运行命令”:
文件_path.attr('type','text');
打破
案例“”:
file_path.hide();
打破
}
});

因此,只需在您想要的标记中使用
,但不要忘记打开和关闭php标记

如何在java脚本中使用它?你能在JS中使用php标记吗?是的,我会写一个例子好的,谢谢,我真的只是添加了php标记,它破坏了JS代码。我如何设置代码这部分的id:var alarmInput=$(“#alarm_action”)<代码>变量alarmInput=$('')将#alarm_操作替换为php echo id