PHP passing variable id through href

后端 未结 4 1567
一向
一向 2020-12-10 20:08

I want to have a link to another page, with particular subject, How can I pass the id ?


        &         
相关标签:
4条回答
  • 2020-12-10 20:52

    Yes you can, it will be considered as 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.

    0 讨论(0)
  • 2020-12-10 20:55

    You need to enclose your variable in a PHP tag:

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

    On the subsequent page you retrieve the parameter from the GET array:

    $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
    }
    
    0 讨论(0)
  • 2020-12-10 21:00

    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

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

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

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

    0 讨论(0)
  • 2020-12-10 21:11

    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; ?>
    
    0 讨论(0)
提交回复
热议问题