Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php while循环中保存值_Php_Html - Fatal编程技术网

在php while循环中保存值

在php while循环中保存值,php,html,Php,Html,尝试创建按钮,按钮上的名称为规则,传递的值为desc。但这只是在按钮上添加$name。我做错了什么 <?php $result = mysqli_query($con,"SELECT * FROM db_Rules"); while($row = mysqli_fetch_array($result)) { $desc = $row['desc']; $name = $row['rule']; ?> <button onclick="myFunction

尝试创建按钮,按钮上的名称为
规则
,传递的值为
desc
。但这只是在按钮上添加
$name
。我做错了什么

<?php    
$result = mysqli_query($con,"SELECT * FROM db_Rules");
while($row = mysqli_fetch_array($result)) {
 $desc = $row['desc'];
 $name = $row['rule'];
?>
      <button onclick="myFunction($desc)">$name</button>
<br>
<?php
 }
?>

$name


您必须
回显PHP变量,以便在HTML元素中获取其值

 <button onclick="myFunction(<?php echo $desc;?>)"><?php echo $name;?></button>

您需要在循环时回显结果

<button onclick="myFunction(<?php echo $desc; ?>)"><?php echo $name; ?></button>

PHP是为PHP设计的。用
PHP代码块包装函数和name变量。


您忘了在php变量周围放置php标记

<?php    
$result = mysqli_query($con,"SELECT * FROM db_Rules");
while($row = mysqli_fetch_array($result)) {
 $desc = $row['desc'];
 $name = $row['rule'];
?>
      <button onclick="myFunction(<?php echo $desc;?>)"><?php echo $name;?></button>
<br>
<?php
 }
?>


你没有回应任何东西,只是HTML

<?php    
$result = mysqli_query($con,"SELECT * FROM db_Rules");
while($row = mysqli_fetch_array($result)) {
$desc = $row['desc'];
$name = $row['rule'];
?>
<button onclick="myFunction(<?=$desc?>)"><?=$name?></button>
<br>
<?php
}
?>

试试这个:

<?php    
$result = mysqli_query($con,"SELECT * FROM db_Rules");
while($row = mysqli_fetch_array($result)) {
 $desc = $row['desc'];
 $name = $row['rule'];
?>
  <button onclick="myFunction(<?= $desc?>)"><?= $name?></button>
<br>
<?php
 }
?>



注意:如果您想在html中使用php代码,那么应该始终使用php标记

<?php 
// Write php code here
?>

这就是为什么你应该在下面一行替换这个

<button onclick="myFunction($desc)">$name</button>
$name


使用PHP代码块并回显变量不要忘记
在变量后面。即使它在这种情况下有效,这也是一种良好的做法
<?php echo $desc; ?> 
<?php echo $name; ?> 
<?php    
$result = mysqli_query($con,"SELECT * FROM db_Rules");
while($row = mysqli_fetch_array($result)) {
 $desc = $row['desc'];
 $name = $row['rule'];
?>
  <button onclick="myFunction(<?= $desc?>)"><?= $name?></button>
<br>
<?php
 }
?>
<?php    
$result = mysqli_query($con,"SELECT * FROM db_Rules");
while($row = mysqli_fetch_array($result)) {
 $desc = $row['desc'];
 $name = $row['rule'];
?>

  <button onclick="myFunction('<?php echo mysql_real_escape_string($desc)?>')"><?php echo $name;?></button>  
  <br />

<?php }?>
<?php 
// Write php code here
?>
<button onclick="myFunction($desc)">$name</button>
<button onclick="myFunction(<?php echo $desc; ?>)"><?php echo $name; ?></button>
echo '<button onclick="myFunction('.$desc.')">'.$name.'</button>';