Issue
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!
<?php
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'];
}
if(isset($_POST['bid-btn'])){
unset($_POST['bid-btn']);
$_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");
}
HTML
<form action="itemInfo.php" method="post">
<div class="place-bid-form">
<h6>Place your bid</h6>
</div>
<div class="place-bid-form-container">
<div class="bidamount-icon">RM</div>
<input class="placebidInput" type="number" name="bid_amount">
</div>
<div class="place-bid-form">
<button type="submit" name="bid-btn" class="btn bid-btn">Bid Now</button>
</div>
</form>
Solution
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">
...
</form>
or you can set the item id dynamically via JavaScript/jQuery like this before you submit your form:
$('button[name="bid-btn"]').click(function(e){
e.preventDefault();
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)