是否可以为HTML输入按钮提供动态名称?

时间:2013-09-18 07:04:28

标签: php html dynamic

这是我目前的代码:

<?php    
    for ( $i = 1; $i <= 9; $i++ ) {
?>

<form action="kill_threads.php" method="POST" >
     <label> 
          <?php echo "<br/><br/>Thread ".$i;?>
          <input type="submit" name = " <?php echo "thread".$i;} ?> " /> 
     </label>
     <input type="submit" name="test" />
</form>

<?php
     for ( $i = 1; $i <= 9; $i++ ) {
         $thread_name = "thread" . $i;
         if ( isset( $_POST[thread_name] ) ) echo "im a killed thread now";
     }
?>

如果我点击它们,我的按钮不起作用,因为我知道我做错了什么。请提前帮助,谢谢。

2 个答案:

答案 0 :(得分:0)

请进行以下更改,然后才能使用

<form action="kill_threads.php" method="POST" >
<?php 
  for ($i=1; $i<=9; $i++) { ?>
    <label> 
            <?php echo "<br/><br/>Thread ".$i;?>  
            <input type="submit" name="<?php echo  "thread".$i; ?>" /> 
    </label>
<?php
  } ?>
 <input type="submit" name="test" />
</form>

<?php
   for ($i=1; $i<=9; $i++) {
     $thread_name = "thread".$i;
     if ( isset($_POST[$thread_name] ) ) echo "im a killed thread now";
   } ?>

修改

早期代码在此行中有for循环结束括号

    <input type="submit" name="<?php echo  "thread".$i; } ?>" /> 

我只是删除了结束括号}并将其放在

之后
   </label>

答案 1 :(得分:0)

试试这个

似乎你会生成多种形式

<form action="kill_threads.php" method="POST" >
<?php     
    for ( $i = 1; $i <= 9; $i++ ) 
    {
          echo "<lable>";
          echo "<br/><br/>Thread ".$i;
  ?>
          <input type="submit" name = " <?php echo "thread".$i; ?> " /> 
<?php
          echo "</lable>";
    }
     </label>
     <input type="submit" name="test" />
</form>

// php script
<?php
     for ( $i = 1; $i <= 9; $i++ ) {
         $thread_name = "thread" . $i;
         if ( isset( $_POST[thread_name] ) ) echo "im a killed thread now";
     }
?>
相关问题