【问题标题】:Issues with 'checked' attribute in 'input' form tags within PHP while loopsPHP while 循环中“输入”表单标签中的“已检查”属性问题
【发布时间】:2014-10-10 23:45:18
【问题描述】:

我正在使用 html 表单来编辑 sql db,因此我希望表单显示数据库中当前的值。我正在使用 while 循环来显示所有 sql 行。我简单的单选按钮允许用户在“列表”(在 sql 中显示为“0”)或“最近的交易”(在 sql 中显示为“1”)之间进行选择。

单选按钮未从已检查或未检查的 sqlas 中预填充值

<form name="edit listing" action="edit_list.php" method="post" id="edit_form" >
  <ul>
       <?php while ($data=mysqli_fetch_assoc($result)):
           $transaction = $data['transaction'];
           $chkvalue='checked="checked"'; ?>
   <li>
    <fieldset>
      <legend>Designation <h6>Required</h6></legend>
       <input name="transaction" type="radio" tabindex="11" value="0" <?php if ($transaction == '0') echo $chkvalue; ?> />
      <label for="listings">Listings</label>
       <input name="transaction" type="radio" tabindex="12" value="1" <?php if ($transaction == '1') echo $chkvalue; ?> />
      <label for="recent_transactions">Recent Transactions</label>
    </fieldset>
    <input type="submit" formaction="edit_list.php" value="Submit Changes" />
</form>
   </li>
           <?php endwhile;mysqli_close($con);?>
 </ul>

简短的 php 插入工作,一点点。我的源代码看起来像这样:

<input name="transaction" type="radio" tabindex="11" value="0"  />
 <label for="listings">Listings</label>
<input name="transaction" type="radio" tabindex="12" value="1" checked="checked" />
 <label for="recent_transactions">Recent Transactions</label>

但单选按钮没有预填充。

我整天都在研究这个问题,这里有任何可能出现问题的建议都会非常有帮助

【问题讨论】:

  • 你必须只写checked 而不是check="checked"
  • 这也没有填写
  • 做一个var_dump($transaction); 告诉我们你得到了什么,这样我们可以更好地帮助你
  • 按原样处理您的 HTML 我在默认情况下选中选中的单选按钮没有任何问题。
  • @Crackertastic,我现在看到它必须与如何使用
    设置我的循环有关,无线电检查出现在页面上的最后一个,但在此之前没有

标签: php forms input checked


【解决方案1】:

你只需要写checked 而不是check="checked"

所以改成这样:

$chkvalue='checked'; ?>

而不是这个:

$chkvalue='checked="checked"'; ?>

【讨论】:

  • 这里来自源代码:
  • @rhill45 你确定吗?如果我复制这个:&lt;input name="transaction" type="radio" tabindex="12" value="1" checked /&gt; 在一个 html 文件中它对我来说很好!
  • 这与我的循环有关,我只是注意到它在我的最后一项中显示了一个选中的值,但在那之前没有
  • @rhill45 您列出的项目 (li) 也超过了结束表单标签!?应该是这样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 2012-10-05
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多