[SOLVED] Calling a GET variable in a POST function


I wanted to add the $item_id into the ‘bid’ table after clicking on the button.
But the $item_id from the $_GET cannot be passed into the $_POST function.
Is it incorrect to call a variable like this?

Thank you in advance for your help!

include ('config/dbfunctions.php'); 
include ("helpers/validateBid.php");

$user_id = "";
$item_id = "";
$table= 'bid';
$allBids = selectAll($table);
$items = selectAll('item');

if (isset($_GET['item_id'])) {
    $itemInfoPost  = selectOne('item', ['item_id' => $_GET['item_id']]);
    $item_id = $itemInfoPost['item_id'];

        $_POST['user_id'] = $_SESSION['user_id'];
        $_POST['item_id'] = $item_id;   //$item_id cannot be called
        $bid_id = create($table, $_POST);   
        header('location: itemInfo.php?item_id='."$item_id"); 


      <form action="itemInfo.php" method="post">
        <div class="place-bid-form">
          <h6>Place your bid</h6>

        <div class="place-bid-form-container">
          <div class="bidamount-icon">RM</div>
          <input class="placebidInput"  type="number" name="bid_amount">

        <div class="place-bid-form">
          <button type="submit" name="bid-btn" class="btn bid-btn">Bid Now</button>


Please make sure that the action attribute of your form has the item id like this:

<form id="mybidform" action="itemInfo.php?item_id=123456" method="post">

or you can set the item id dynamically via JavaScript/jQuery like this before you submit your form:

    let actionWithItemId = $("#mybidform").prop("action") + "?item_id=" + 123456;
    $("#mybidform").prop("action", actionWithItemId).submit();


If you don't use jQuery, I would post plain Javascript code also.

Answered By – Selim Acar

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *