[英]Radio Button inside Foreach Loop -PHP
如何在foreach循環中保留選中的單選按鈕? 我正在開發一個Web應用程序,該應用程序將首先檢查所選服務是否可用。
這是我當前的foreach循環代碼:
<h4>-- Touch Therapies --</h4>
<?php
$contents = get_service_touch(); //get_service_touch() = SELECT query to get touch therapies
foreach($contents as $content) {
?>
<input type="radio" name="content_id" value="<?php echo $content['content_id']; ?>"
<?php
if(isset($_POST['content_id'])){
echo 'checked = "checked"';
}
?>>
<?php echo nl2br($content['content']);?>
<font color="red"><b>P<?php echo nl2br($content['price']);?></b></font><br>
<?php
} //endforeach;
?>
但是,即使我選擇其他服務,當我單擊“檢查可用性” //((提交按鈕))時,它始終會檢查最后一個單選按鈕。
foreach輸出的圖片: http : //oi61.tinypic.com/rrnujd.jpg
如果content_id在POST中,則選中的屬性將添加到所有輸入中。 我認為您需要在條件中添加等式。
if (isset($_POST['content_id']) && $_POST['content_id'] === $content['content_id']) {
// echo checked
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.