如何在另一个页面中发送带有foreach循环和特定ID的数据?

发布于 2025-01-17 10:38:34 字数 2819 浏览 1 评论 0原文

我在php和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
                    }
                    }?>

并且按钮中使用的javascript:

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

在开始时,我将通过复选框接收“服务1”选项,以继续进行上面显示的脚本,当我想去时出现问题。从3个显示出在FPDF中生成报告的ID,FPDF中的脚本如下:

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

当我按下按钮时,它不会根据与选项同时选择的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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2025-01-24 10:38:34

您可以以搜索参数的形式添加值。
这些参数都具有

搜索参数的开始由
然后指定 value 与等价符号key = value
键值对通过&amp;符号分开。

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

您会注意到,单击链接时,值将传递到URL。服务器能够读取这些值。

中ernospage.php使用 superglobal $ _ get 访问URL中的值。 $ _ get关联阵列将数据呈现。这意味着您可以通过在数组中访问来获得一个值,如下面的示例。

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

此方法可以暴露敏感数据。因此,请确保不要将任何数据诸如密码之类的数据打印到前端,以供人们查看。将这些数据保留在后端。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文