在POST函数中调用GET变量



我想在点击按钮后将$item_id添加到"bid"表中。但是$_GET中的$item_id不能传递到$_POST函数中。这样调用一个变量不正确吗?

提前感谢您的帮助!

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

请确保表单的action属性具有如下项目id:

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

或者您可以在提交表单之前通过JavaScript/jQuery动态设置项目id:

$('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.

相关内容

  • 没有找到相关文章

最新更新