简体   繁体   中英

PHP get information from database when echoed html code is clicked

In my code, I am retrieving data from the database using a while loop so i can have all the $FormData['fullname'] in the db.

What i want to do is, have each name display on the page and also have clickable so when someone clicks a name, it gets their user_id and pulls up information about them.

The problem i am having is that I can't figure out a way to make a it so where i can get the user_id when the user clicks a name. I tried putting a "name" in the button attribute and I checked if isset() but that didn't work.

If someone can properly figure out a way for me to basically, display all the fullnames in my database and when someone clicks a name, it pulls up information about them that is stored in the database. Here is my code

$stmtGet = $handler->prepare("SELECT * FROM formdata");
$stmtGet->execute();
while($formData = $stmtGet->fetch()){

    echo "<button name='name'>$formData[fullname]</button>";
    if($_SERVER['REQUEST_METHOD'] =="POST"){

        if(isset($_POST['name'])){

            echo "ok";
        }else{

            echo "bad";
        }
    }

}

I should stress that this is not production code and you should totally validate the data input coming in before posting queries to your DB. You can do something like this.

<?php
    // Connect
    $connection = mysqli_connect('localhost', 'username', 'password', 'database','port');
    // Grab all users
    $sql = 'SELECT * FROM users';
    $users = mysqli_query($connection, $sql);

    if (($_SERVER['REQUEST_METHOD'] == 'POST') && !empty($_POST['user_id'])) {
        $query = "SELECT * FROM users WHERE user_id = {$_POST['user_id']};";
        $user = mysqli_fetch_assoc(mysqli_query($connection, $query));
    }
?>

// This only runs if our $user variable is set.
<?php if (isset($user)) : ?>
    <?php foreach ($user as $key => $value) : ?>
        <span><?= print_r($value) ?></span>
    <?php endforeach; ?>
<?php endif; ?>

// Display all users in a dropdown and when the button is clicked
// submit it via post to this page.
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
    <select name="user_id">
        <?php foreach ($users as $user) : ?>
            <option value="<?= $user['user_id'] ?>"><?= $user['name'] ?></option>
        <?php endforeach; ?>
    </select>
    <button type="submit">Submit</button>
</form>

This is going to refresh your page every time. If you want to have an interactive page you are going to need to use JavaScript/AJAX to update the page elements without reloading the page. This example just demonstrates how you can achieve this with PHP and HTML.

As far i can see you are trying hit a button inside the while loop , i would not say its a bad approach , but i will suggest you not to do that . and from your code i can see you have lack of understanding post and get request learn from here . and other than this you need to know the transition of web url . how its actually works . anyway , i have given a sample code without . i hope it will help you understanding this concept.

  $stmtGet = $handler->prepare("SELECT * FROM formdata");
   $stmtGet->execute();
while($formData = $stmtGet->fetch(PDO::FETCH_ASSOC)){
$id  = $formData['formdataid'];
 echo "<a href='somepagename.php?infoid={$id}'>". $formData['fullname']."</a></br>";
}

now in the somepagename.php file or in the same page you can actually show the details information for instance

if(isset($_GET['infoid'])){
$stmt = $handler->prepare("select * from formdata where formdataid='"$_GET['infoid']"'");
$qry = $stmt->execute();
$row = $qry->fetch(PDO::FETCH_ASSOC);
echo "<p>id =".$row['formdataid']."</p></br>";
echo "<p>id =".$row['name']."</p></br>";
echo "<p>id =".$row['email']."</p></br>";
echo "<p>id =".$row['address']."</p></br>";

code is not executed , it may have semicolon or comma error warning . you have to fix those on your own . this example above shown you only the way it works . if still you have problem ask , or see the documentation

you need to know about server-side language (php) and client-side language (javascript).

php runs before page loaded. it cannot runs when click something by itself(with ajax, it can). most interactions without page move runs with javascript.

in your case, there are two methods.

  1. store all information into hidden input or button's attribute. and get them by user_id via javascript.
  2. use ajax to call another php that can select informations you need by user_id.

both use javascript or jquery. so you must learn about them.

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