简体   繁体   中英

How to get the selected data from a loop with submit button using PHP

I have use loop to view all current data userID and name I've limit the information about the user. And now it has a button in each of them to click to view more information about the user. Now I'm Stuck here I don't know how to get the selected data to view it in viewDetails.php. I want to use form and I don't know what to put inside the form so i will have identifier to query select in viewDetails.php

Here are my codes in selected.php

<?php
    if($result = $db->query("SELECT userID, name FROM user ")) {     
            while($row = $result->fetch_assoc())    {
                echo '<form method="POST" action="viewDetails.php"> ';

                    echo $row['userID'], '. ';
                    echo $row['name'];
                    echo '<input type="hidden" name="userid" value="'.$row['userID'].'"><br>';
                    echo '<input type="submit" name="submit" value="View More Details"><br>';
                echo '</form>';
            }
        }else{
            die($db->error);
        }
?>

Heres my code to view the selected data 'viewDetails.php'

    <?php
if(isset($_POST['userid'])){ 
$selected = $_POST['userid'];
    if($result = $db->query("SELECT * FROM user WHERE userID=$selected ")) {     
            while($row = $result->fetch_assoc())    {

                echo $row['name'], '&nbsp;-&nbsp;&nbsp; ';
                echo $row['email'], '&nbsp;-&nbsp;&nbsp; ';
                echo $row['address'], '<br>';

            }
        }else{
            die($db->error);
        }
}
?>

Add a hidden field with the users ID:

echo '<input type="hidden" name="userid" value="'.$row['userID'].'"><br>';

And then you can retrieve the ID on the PHP page by doing

"SELECT * FROM user WHERE id=".$_POST['userid'].""

Thinking further, it could be worth storing all of the users data in hidden fields (but still display the ID and name) and post them to the PHP page. It would then prevent you from needing to query your database again, as you could get all of the data from the first query (if you change it to SELECT *):

echo '<input type="hidden" name="userid" value="'.$row['userID'].'"><br>'
echo '<input type="hidden" name="name" value="'.$row['name'].'"><br>'
echo '<input type="hidden" name="email" value="'.$row['email'].'"><br>'
echo '<input type="hidden" name="address" value="'.$row['address'].'"><br>'

Change the query .. "SELECT * FROM user WHERE userID=$selected"

<?php


   if($result = $db->query("SELECT userID, name FROM user ")) {     
            while($row = $result->fetch_assoc())    {
                echo '<form method="POST" action="viewDetails.php"> ';

                    echo $row['userID'], '. ';
                    echo $row['name'];
                    echo '<input type="hidden" name="userid" value="'.$row['userID'].'"><br>';
                    echo '<input type="submit" name="submit" value="View More Details"><br>';
                echo '</form>';
            }
        }else{
            die($db->error);
        }
?>
<?php
if(isset($_POST['userid'])){ 
$selected = $_POST['userid'];
    if($result = $db->query("SELECT * FROM user WHERE userID=$selected ")) {   
            while($row = $result->fetch_assoc())    {

                echo $row['name'], '&nbsp;-&nbsp;&nbsp; ';
                echo $row['email'], '&nbsp;-&nbsp;&nbsp; ';
                echo $row['address'], '<br>';

            }
        }else{
            die($db->error);
        }
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM