简体   繁体   English

PHP通过href传递变量id

[英]PHP passing variable id through href

I want to have a link to another page, with particular subject, How can I pass the id ?我想要一个链接到另一个页面,具有特定的主题,我如何传递 id ?

<table class="table table-hover">
    <thead>
        <tr>
            <th>Subject</th>
            <th>Add Topic</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <?php foreach($subjects as $sub):?>
            <td><?php echo $sub->subject;?></td>
            <td><a href='approve.php?id=".$sub->id."' role="button" class="btn">Add Topic</a></td>
        </tr>
        <?php endforeach;?>
    </tbody>
</table>

You still need to echo it out.您仍然需要将其回显出来。

<?php foreach($subjects as $sub): ?>
    <tr>
    <td><?php echo $sub->subject ?></td>
    <td><a href="approve.php?id=<?php echo $sub->id ?>" role="button" class="btn">Add Topic</a></td>
    </tr>
<?php endforeach; ?>

Please try the following:请尝试以下操作:

<table class="table table-hover">

    <thead>
    <tr>
        <th>Subject</th>
        <th>Add Topic</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach($subjects as $sub):?>
    <tr>


        <td><?php echo $sub->subject;?></td>
        <td><a href='approve.php?id=<?php echo $sub->id; ?>' role="button" class="btn">Add Topic</a></td>

    </tr>
    <?php endforeach;?>
    </tbody>
</table>

and then on the page : approve.php然后在页面上:approve.php

<?php
$subjectId  = $_GET['id'];
?>

$subjectId will give you the corresponding subject id with which you can move forward with the functionality. $subjectId将为您提供相应的主题 ID,您可以使用该 ID 继续使用该功能。

Note: foreach should start either outside <tr> and end outside </tr> or it can be inside <tr> </tr>注意: foreach应该在<tr>之外开始并在</tr>之外结束,或者它可以在<tr> </tr>

You need to enclose your variable in a PHP tag:您需要将变量括在 PHP 标记中:

<?php foreach($subjects as $sub):?>
  <tr>
     <td><?php echo $sub->subject;?></td>
     <td><a href='approve.php?id=<?php echo $sub->id ?>' role="button" class="btn">Add Topic</a></td>

   </tr>
<?php endforeach;?>

There is also a short form echo tag enabled on most PHP servers <?= $variable ?>大多数 PHP 服务器上还启用了一个简短的 echo 标记<?= $variable ?>

On the subsequent page you retrieve the parameter from the GET array:在随后的页面上,您从GET数组中检索参数:

$subject = $_GET['id'];

If you're passing this value to the database you should do some validation:如果您将此值传递给数据库,您应该进行一些验证:

if ($_GET['id']) { // check parameter was passed
  $subject = int_val($_GET['id']) // cast whatever was passed to integer
} else {
  // handle no subject case
}

Yes you can, it will be considered as GET.是的,您可以,它将被视为 GET。 as for how to pass it.至于怎么通过。 Edit the following:编辑以下内容:

 <td><a href='approve.php?id=<?=$sub->id?> ' role="button" class="btn">Add Topic</a></td>

This is the part:这是部分:

 <?=$sub->id?>

You closed php tag when u started adding html therefore open it again to echo php vars.当你开始添加 html 时,你关闭了 php 标签,因此再次打开它以回应 php vars。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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