[英]PHP: My button doesn't properly work inside a foreach loop
我有這個find_all()函數,它寫在一個單獨的文件中:
public static function find_all() {
return self::find_by_sql("SELECT * FROM ".self::$table_name);
}
它在包含我的foreach循環的文件頂部被引用:
<?php require_once("../../includes/initialize.php"); ?>
<?php if (!$session->is_logged_in()) { redirect_to("login.php"); } ?>
<?php
$parents = UserParent::find_all();
?>
這是foreach循環:
<?php foreach($parents as $parent): ?>
<div class='popup-screen' id = "popup">
<div class = "spacing">
Do you want to delete this data?
</div>
<a href="list_users.php?parentNum=<?php echo $parent->parentNum; ?>"> <input type="button" value="YES" class = "popup-button"> </a>
<input type="button" value="CANCEL" class = "popup-button" onClick = "hide();">
</div>
<tr class = "tr-1">
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';"><img src="../<?php echo $parent->image_path(); ?>" width="100" height = "100" class = "profile-pic"/></td>
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';">Parent</td>
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';"><?php echo $parent->username; ?></td>
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';"><?php echo ucwords($parent->firstName); ?></td>
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';"><?php echo ucwords($parent->lastName); ?></td>
<td onClick = "show();"><img src = "../stylesheets/images2/delete-icon.png" height="25" width="25" ></td>
</tr>
<?php endforeach; ?>
這是javascript代碼:
function show()
{
document.getElementById("popup").style.display='block';
}
function hide(){
document.getElementById("popup").style.display='none';
}
基本上,我的代碼所做的是創建從foreach循環獲取其數據的信息行。 在每行的末尾是一個刪除圖標,如img標記所示。 點擊刪除圖標后,show()函數將運行(show()函數只顯示彈出div,它是不可見的) - 確認用戶是否想要刪除他/她的數據。 如果用戶單擊CANCEL,則窗口將關閉,如javascript代碼所示。 如果用戶單擊是,則支持轉到鏈接:list_users.php?parentNum = parentNum; ?>($ parent-> parentNum的值對於每一行都不同)。 但是,錨標記ALWAYS會檢索第一行的鏈接,無論它是第三行還是其他(順便說一下,其他td標記上的鏈接都可以工作)。 現在,我的問題是,如何正確鏈接彈出窗口上每行的YES按鈕?
$ parent-> parentNum的值對於每一行都是不同的
但在你的代碼中,它不是:
<?php echo $parent->parentNum; ?>
該值不會遞增或更改,也不會對所有行都相同。
另外,我強烈建議使用<button>
或<a>
元素。 這就是他們的目的。 <td>
s並不意味着被點擊。 良好的設計實踐(幾乎)總是值得花一點額外的努力。
IMO,你的代碼看起來應該更像
...
<td><a href='viewParent.php?n=<?php echo $n; ?>'></a></td>
...
<button name='show' value='1'>Show</button>
...其中$n
來自for循環。 然后在頁面頂部
if (isset($_POST['show'])) { ...logic... }
接收你的'節目'活動。
<?php
$parentNum = 0;
foreach($parents as $parent): ?>
<div class='popup-screen' id = "popup">
<div class = "spacing">
Do you want to delete this data?
</div>
<a href="list_users.php?parentNum=<?php echo $parent[$parentNum]; ?>"> <input type="button" value="list_users.php?parentNum=<?php echo $parent[$parentNum]; ?>" class = "popup-button"> </a>
<input type="button" value="CANCEL" class = "popup-button" onClick = "hide();">
</div>
<tr class = "tr-1">
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent[$parentNum]; ?>';"><img src="../<?php echo $parent->image_path(); ?>" width="100" height = "100" class = "profile-pic"/></td>
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent[$parentNum]; ?>';">Parent</td>
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent[$parentNum]; ?>';"><?php echo $parent[$parentNum]->username; ?></td>
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent[$parentNum]; ?>';"><?php echo ucwords($parent[$parentNum]->firstName); ?></td>
<td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent[$parentNum]; ?>';"><?php echo ucwords($parent[$parentNum]->lastName); ?></td>
<td onClick = "show();"><img src = "../stylesheets/images2/delete-icon.png" height="25" width="25" ></td>
</tr>
<?php
$parentNum++;
endforeach; ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.