PHP: My button does not work correctly inside the foreach loop

I have a find_all () function, which is written in a separate file:

public static function find_all() { return self::find_by_sql("SELECT * FROM ".self::$table_name); } 

It was listed at the top of the file, which includes my foreach loop:

 <?php require_once("../../includes/initialize.php"); ?> <?php if (!$session->is_logged_in()) { redirect_to("login.php"); } ?> <?php $parents = UserParent::find_all(); ?> 

This is the foreach loop:

 <?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; ?> 

And this is the javascript code:

 function show() { document.getElementById("popup").style.display='block'; } function hide(){ document.getElementById("popup").style.display='none'; } 

Basically, my code is creating lines of information that gets its data from the foreach loop. At the end of each line is a delete icon, as shown by the img tag. After clicking on the delete icon, the show () function will be launched (the show () function only shows a pop-up window that is invisible) - confirmation of whether the user wants to delete his data or not. If the user clicks the CANCEL button, the window will close as shown by the javascript code. If the user clicks "YES", SUPPOSED goes to the link: list_users.php? ParentNum = parentNum ;? > (The value of $ parent-> parentNum is different for each row). However, the linked ALWAYS tag retrieves the link for the first line, regardless of whether it is the third line or something (by the way, links to other td tags work). Now, to my question, how to properly link the YES button for each row in a popup div?

+7
source share
2 answers

The value of $ parent-> parentNum is different for each row.

But in your code this is not:

 <?php echo $parent->parentNum; ?> 

The value does not increase or change or nothing changes the same for all rows.


In addition, I highly recommend using the <button> or <a> elements for this. This is what they are made for. <td> should not have been pressed. Good design practice is (almost) always worth a little extra effort.

IMO, your code should be more like

 ... <td><a href='viewParent.php?n=<?php echo $n; ?>'></a></td> ... <button name='show' value='1'>Show</button> 

... where $n is from the for loop. Then at the top of the page

 if (isset($_POST['show'])) { ...logic... } 

to get your show event.

+4
source
 <?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; ?> 
+2
source

All Articles