Skip to content
Advertisement

How do I pass a variable containing a value to another page via a link (in tag ) in PHP using the functionality of GET method

Hope you are having an excellent day 🙂 I’m learning to build my website,it’s just a beginning.

$product_id=10;
echo "<tr><td>Total</td><td>Rs/-" . $sum . "</td><td><a href='success.php?id='$product_id'' class='btn btn-primary'>Confirm Order</a></td></tr>";

Now, in this code, I’ve been trying to pass the value of $product_id variable to my success.php page but the page receives null when I access the value, so, How do I pass this value to my success.php page using the variable.

Thank You.

Advertisement

Answer

Your single quotes are causing the href attribute to end at id=. Investigate the HTML in your browser, you’ll see something like this:

<tr>
  <td>Total</td>
  <td>Rs/-1.23</td>
  <td>
    <a href='success.php?id=' $product_id'' class='btn btn-primary'>
      Confirm Order
    </a>
  </td>
</tr>

I recommend using curly braces to place variables inside of quoted content:

echo "<tr><td>Total</td><td>Rs/-{$sum}</td><td><a href='success.php?id={$product_id}' class='btn btn-primary'>Confirm Order</a></td></tr>";
Advertisement