如何解决如何在PHP中从另一个页面插入数据
我正在尝试将项目插入购物车表并通过插入页面传递一些信息,但它需要我到 addTo.php 页面并打印“无需处理”,我可以理解它无法识别按钮和表单来自上一页。
这是 item_details 页面,当我按下 addtocart 按钮时,我想在其中获取 item_id 和数量:
<section class="main-content">
<div class="row">
<?php
try{
if (isset($_GET["iid"])){
$iid=$_GET["iid"];
require('connection.php');
$sql =$db->prepare( "SELECT * FROM items WHERE item_id = ?");
$sql->execute(array($iid));
foreach($sql as $r){
?>
<div class="span9">
<div class="row">
<div class="span4">
<a href="images/<?php echo $r['item_photo']; ?> " class="thumbnail" data-fancybox-group="group1" title="<?php echo $r['item_name'];?>"><img alt="" src="images/<?php echo $r['item_photo']; ?>"></a>
</div>
<div class="span5">
<address>
<strong>Name:</strong> <span><?php echo $r['item_name'];?></span><br>
<strong>Brand:</strong> <span><?php echo $r['item_brand'];?></span><br>
<strong>Availability:</strong>
<span><?php
if ($r['item_qty']==0){
echo"Out Of Stock";
}else {
echo"Avilable";
}
?></span><br>
</address>
<h4><strong>Price: <?php echo "BD".$r['item_price'] ; ?></strong></h4>
</div>
<div class="span5">
<form class="form-inline" method="post" action="addTo.php">
<input type="hidden" name="itemID" value="<?php echo $r['item_id'];?>">
<p> </p>
<label>Qty:</label>
<input type="text" class="span1" name="itempqty" value= "<?php echo $r['item_qty'];?>">
<button class="btn btn-inverse" type="button" name="addtocart" onclick="window.location.href='addTo.php?iid=<?php echo $r['item_id'];?>'" >Add to cart</button>
<button class="btn btn-inverse" type="button" name="addtowishlist" onclick="window.location.href='addTo.php?iid=<?php echo $r['item_id'];?>'" >Add to wishlist</button><br><br>
</form>
<?php
//if (isset($_SESSION['UserType'])) {
//if ($_SESSION['UserType'] == "staff"||$_SESSION['UserType'] == "admin") {?>
<form class="form-inline" method="post">
<input type="hidden" name="itemID" value="<?php echo $r['item_id'];?>">
<button class="btn" type="button" name="deleteItem" onclick="window.location.href='ManageOffers.php?iid=<?php echo $r['item_id'];?>'" >Manage Offer</button>
<button class="btn" type="button" name="updateItem" onclick="window.location.href='updateItem.php?iid=<?php echo $r['item_id'];?>'" >Manage Item</button>
<?php //}} ?>
</form>
</div>
</div>
</div>
<?php
}}
$db=null;
}
catch(PDOException $ex){
die ($ex->getMessage());
}
?>
</div>
</section>
And this is addTo.php where I want to insert item to the cart table:
<?php
session_start();
extract($_POST);
extract($_GET);
if(isset($_POST["addtocart"])){
try{
if (isset($_GET["iid"])){
$iid=$_GET["iid"];
$itemID = $_POST['itemID'];
$itemqty = $_POST['itempqty'];
$uid='1';
//$cart_id='1';
require('connection.php');
$sql=$db->exec("INSERT INTO cart VALUES(null,'$itemID','$itemqty','$uid')");
if ($sql)
{
echo"<script>alert('item has been succefuly added to the cart')</script>";
header('location:index.php');
}
else
{
error("Failed");
}
}
$db=null;
}
catch(PDOException $e){
die($e->getMessage());
}
}
else {
echo"no process to do";
}
?>
这也是我按下按钮时显示的内容:
你能看一下并帮助我如何解决问题吗?
解决方法
据我所知,您不能发送按钮名称。您可以发送 input、select 和 textarea 值。
为什么要使用 onclick 事件来更改您的位置?使用形式和行动。当您提交表单时,您会将整个表单中的所有数据发送到另一个页面。
我们从输入、文本区域和选择(选项)接收所有数据。
<form class="form-inline" method="post" action="addTo.php">
<input type="hidden" name="itemID" value="<?php echo $r['item_id'];?>">
<p> </p>
<label>Qty:</label>
<input type="text" class="span1" name="itempqty" value= "<?php echo $r['item_qty'];?>">
<input type="hidden" name="addtocart" value="<?php echo $r['item_id'];?>:>
<button class="btn btn-inverse" type="button">Add to cart</button>
<button class="btn btn-inverse" type="button" name="addtowishlist" onclick="window.location.href='addTo.php?iid=<?php echo $r['item_id'];?>'" >Add to wishlist</button><br><br>
</form>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。