Skip to content
Advertisement

How to send data in another page with a foreach loop and specific ID?

i have the follow code in PHP and HTML:

 <?php if(isset($_POST['service1'])){
                        $sql = "SELECT u.id u.name, u.password FROM users u";
                        $result = conection($sql);
                        $data["tittle"] = "result";
                        $data["result"] = $result;
                        foreach($data["result"]as $smth){
                        $id = $smth["id"];
                        $name = $smth["name"];
                        $password = $smth["password"];
                    ?>
                        <table style="width:100%">
                        <div class="text-center">
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Password</th>
                            </tr>
                            <tr>
                                <td><?php echo $id;?></td>
                                <td><?php echo $name;?></td>
                                <td><?php echo $password;?></td>
                                <td><a role="button" href='anotherpage.php' onclick="documents('<?= $id?>')" class='btn btn-primary' target="_blank"></td></tr>
                                </a>
                    <?php
                    }
                    }?>

And the JavaScript used in the button:

<script>
      function documents($id)
  {
       document.getElementById($id);
  }
   </script>

In the beginning I am receiving the option of “Service 1” through a checkbox to proceed with the script I showed above, the problem arises when I want to select an ID from the 3 that are shown to generate a report in FPDF, the script in FPDF is as follows:

<?php
        ob_start();
        require ($_SERVER['DOCUMENT_ROOT']."/Libraries/fpdf.php");
        class PDF extends FPDF
        {
            function Header()
            {
                
                $this->SetFillColor(0,90,151);
                $this->SetTextColor(0);
                $this->SetDrawColor(0,90,151);
                $this->SetLineWidth(.50);
                $this->SetFont('','B');
                $this->Ln(50);
                $this->SetFont('Arial','B',20);
                $this->Cell(3);
                $this->Rect(10,55,260,25);
                $this->Ln(10);
                $this->Cell(3);
                $this->Cell('0','0',$id." ".$name. " ".$password,0,0,'L');
                $this->Ln(20);
                
            }
        }

        $pdf=new PDF('L','mm','A4');
            $pdf->SetFont('Arial','',12);
            $pdf->AddPage();
            $pdf->Output();
        ?>

When I press the button it does not bring me the data depending on the ID that is selected at the same time as the option, does anyone know why this happens? or if I’m failing at something

Advertisement

Answer

You can add values to a link in the form of search parameters.
These parameters all have a key and a value.

The start of the search parameters is marked by a ?.
Then specify the key and the value combined with an equals symbol key=value.
Key-value pairs are separated by an & symbol.

<a role="button" href="anotherpage.php?id=<?= $id ?>&name=<?= $name ?>" class='btn btn-primary' target="_blank">

You’ll notice that when clicking the link the values are passed along to the URL. The server is able to read these values.

In anotherpage.php use the superglobal $_GET to access the values that are in the URL. $_GET presents the data as an associative array. This means that you can get a value by accessing the key in the array, like the example below.

$id = $_GET['id'];
$name = $_GET['name'];

This method can expose sensitive data. So be sure to not print any data like passwords to the frontend for people to see. Keep that data on the backend.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement