php PHP通过href传递变量id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20967931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:42:43  来源:igfitidea点击:

PHP passing variable id through href

php

提问by Rimjhim Ratnani

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>

回答by xdazz

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

回答by Kumar Saurabh Sinha

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'];
?>

$subjectIdwill give you the corresponding subject id with which you can move forward with the functionality.

$subjectId将为您提供相应的主题 ID,您可以使用该 ID 继续使用该功能。

Note: foreachshould start either outside <tr>and end outside </tr>or it can be inside <tr> </tr>

注意:foreach应该从外面开始并在外面<tr>结束</tr>,也可以在里面<tr> </tr>

回答by Pete

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 GETarray:

在随后的页面上,您从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
}

回答by Mohammed Joraid

Yes you can, it will be considered as GET. as for how to pass it. Edit the following:

是的,您可以,它将被视为 GET。至于怎么通过。编辑以下内容:

 <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。